Blog·Tanky WooABOUTRSS

HDOJ 1159 Common Subsequence

29 Jul 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1159

这题不好说啊~~~~

 #include 
using namespace std;
char a[1003], b[1003];
int table[1003][1003];
int _max(int a, int b)
{
    return a>b? a:b;
}
int main()
{
    while(scanf("%s %s", a, b) != EOF)
    {
        int len1 = strlen(a);
        int len2 = strlen(b);
        int flag = 0;
        for(int i=0; i<len1; ++i)    //把第0行初始化
        {
            if(b[0] == a[i])
                flag = 1;
            if(flag == 0)
                table[0][i] = 0;
            else
                table[0][i] = 1;
        }
        flag = 0;
        for(int i=0; i<len2; ++i)   //把第0列初始化
        {
            if(a[0] == b[i])
                flag = 1;
            if(flag == 0)
                table[i][0] = 0;
            else
                table[i][0] = 1;
        }
        for(int i=1; i<len2; ++i)
            for(int j=1; j<len1; ++j)
                if(b[i] == a[j])
                    table[i][j] = table[i-1][j-1]+1;
                else
                    table[i][j] = _max(table[i-1][j], table[i][j-1]);
        /*
        for(int i=0; i<len1; ++i)
        {
            for(int j=0; j<len2; ++j)
                printf("%d ", table[i][j]);
            printf("\n");
        }
        */
        printf("%d\n", table[len2-1][len1-1]);
    }
    return 0;
}