hoj1227 Common Subsequence

/*This Code is Submitted by billforum for Problem 4000104 at 2012-02-01 18:33:20*/
#include <iostream>
#include <cstring>
using namespace std;
int d[1001][1001];

int max(int x,int y)
{
        return (x>y?x:y);
}

int main(int args,char** argv)
{
        char str1[1001],str2[1001];
        while(cin>>str1>>str2)
        {
                int len1,len2;
                len1=strlen(str1);
                len2=strlen(str2);
                
                for(int i=0;i<len1;i++)
                {
                        for(int j=0;j<len2;j++)
                        {
                                d[i][j]=0;
                        }
                }
                
                for(int i=0;i<len1;i++)
                {
                        for(int j=0;j<len2;j++)
                        {
                                int tmp1,tmp2,tmp3;
                                if(i-1>=0) tmp1=d[i-1][j];
                                else tmp1=0;
                                if(j-1>=0) tmp2=d[i][j-1];
                                else tmp2=0;
                                if((i-1>=0)&&(j-1>=0)) tmp3=d[i-1][j-1];
                                else tmp3=0;
                                if(str1[i]==str2[j])
                                        d[i][j]=tmp3+1;
                                else
                                        d[i][j]=max(tmp1,tmp2);
                                
                        }
                }
                cout<<d[len1-1][len2-1]<<endl;
        }
        return 0;
}

 http://acm.hit.edu.cn/hoj/problem/view?id=1227

状态方程

if(str1[i]==str2[j])  d[i][j]=d[i-1][j-1]

else d[i][j]=max(d[i-1][j],d[i][j-1]);

posted @ 2012-02-01 20:08  wuzhibin  阅读(174)  评论(0)    收藏  举报