Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

TLe了 递推写的 哪里还可以优化吗?

Posted by ___A___ at 2009-06-10 15:34:21 on Problem 1404
#include <iostream>
#include <string>
#define SIZE 159
#define MAX 555599999
using namespace std;
struct x
{
    int value;
    string pos;
}f[SIZE][SIZE];
bool used[SIZE][SIZE];

int init_pos,init_id,value[SIZE];
string str_id,str_pos;
int Dis(const int pos_1,const int pos_2)
{
    int result=0,ptr=1;
    for (int i=pos_1;i<=pos_2;i++)
    {
        result+=value[i]*ptr;
        ptr++;
    }
    return result;
}

struct x DP(const int pre_pos, const int pre_id)
{
    if (used[pre_pos][pre_id]) return f[pre_pos][pre_id];

    struct x Min;
    Min.value=MAX;

    Min.pos+=str_pos[pre_pos-1];

    if (pre_id==init_id)
    {
        Min.value=Dis(pre_pos,init_pos);
        return Min;
    }
    string min_str;

    for (int cur_pos=pre_pos+1; cur_pos<=init_pos+1-(init_id-pre_id); cur_pos++)
    {
        struct x cur_min=DP(cur_pos,pre_id+1);
        cur_min.value+=Dis(pre_pos,cur_pos-1);
        if (cur_min.value<Min.value)
        {
            Min.value=cur_min.value;
            min_str=cur_min.pos;
        }
    }
    Min.pos+=min_str;
    //
    f[pre_pos][pre_id]=Min;
    used[pre_pos][pre_id]=true;
    //
    return Min;
}


int main()
{
    int circle;
    scanf ("%d",&circle);
    for (int tmp=1;tmp<=circle;tmp++)
    {
    cin>>init_id>>init_pos;
    cin>>str_id;
    cin>>str_pos;
    for (int i=1;i<=init_pos;i++) cin>>value[i];
    memset(used,false,sizeof(used));

    struct x result=DP(1,1);
    int ptr=0,ptr_2=1;
    printf ("Keypad #%d:\n",tmp);
    for (int i=0;i<init_id;i++)
    {
        printf ("%c: %c",str_id[i],str_pos[ptr++]);

        while (ptr<init_pos&&str_pos[ptr]!=result.pos[ptr_2])
        {
            printf ("%c",str_pos[ptr++]);
        }
        ptr_2++;
        printf ("\n\n");
    }
    //cout<<result.pos+"    "<<result.value<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator