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

简单DP,对第i个位置考虑要不要保留。。。。根据以前的状态推最少需要删除几个

Posted by CKboss at 2013-09-13 16:34:30 on Problem 3267
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n,m;
char wen[500];
int dp[500];

struct dict
{
    char word[100];
    char theLastLaw;
    int len;
}D[800];

int main()
{
    scanf("%d%d",&n,&m);
    getchar();
    for(int i=1;i<=m;i++)
    {
        scanf("%c",&wen[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%s",D[i].word);
        D[i].len=strlen(D[i].word);
        D[i].theLastLaw=D[i].word[D[i].len-1];
    }
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=m;i++)
    {
        dp[i]=dp[i-1]+1;
        for(int j=0;j<n;j++)
        {
            if(wen[i]==D[j].theLastLaw&&i>=D[j].len)
            {
           //     cout<<i<<": "<<wen[i]<<" catch with: "<<D[j].word<<endl;
                int flag=D[j].len-1,delet=0,pos=-1;
                for(int k=i;k>0;k--)
                {
                    if(wen[k]==D[j].word[flag])
                        flag--;
                    else
                        delet++;
                    if(flag<0){ pos=k; break;}
                }
             //   cout<<"the flag: "<<flag<<"  catch end with: "<<pos<<"  , delet is: "<<delet<<endl;
                if(flag<0)
                {
                    if(pos==-1) pos=0;
                    dp[i]=min(dp[i],dp[pos-1]+delet);
                }
            }
        }
    }
    printf("%d\n",dp[m]);
    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