天天看點

解題報告:hdu1159 common consequence LCS裸題

2017-09-02 17:07:42

writer:pprp

通過這個題溫習了一下剛學的LCS

代碼如下:

/*
@theme:hdu1159
@writer:pprp
@begin:17:01
@end:17:06
@declare:LCS的裸題,溫習一下
@error:從1開始讀入的話,用strlen也要從1開始測才可以
@date:2017/9/2
*/

#include <bits/stdc++.h>

using namespace std;

char s1[1010],s2[1010];
int dp[1010][1010];
int main()
{

    //freopen("in.txt","r",stdin);
    while(~scanf("%s%s",s1+1,s2+1))
    {
        memset(dp,0,sizeof(dp));
        int n = strlen(s1+1);
        int m = strlen(s2+1);

        for(int i = 1; i <= n ;i++)
        {
            for(int j = 1; j <= m ; j++)
            {
                if(s1[i] == s2[j])
                    dp[i][j] = dp[i-1][j-1] + 1;
                else
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[n][m] << endl;
    }

    return 0;
}      

代碼改變世界

LCS