您的位置:首页 > 其它

UVa 10192 - Vacation

2014-03-12 22:51 344 查看
题目链接:UVa 10192 - Vacation

LCS。

#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

const int MAX_N = 100 + 10;
int dp[MAX_N][MAX_N];
char a[MAX_N],b[MAX_N];

int main()
{
    int num = 0;
    while(gets(a) && a[0] != '#')
    {
        gets(b);
        int len1 = strlen(a);
        int len2 = strlen(b);
        for(int i = 0;i <= len1;i++)
            dp[i][0] = 0;
        for(int i = 1;i <= len2;i++)
            dp[0][i] = 0;

        for(int i = 1;i <= len1;i++)
        {
            for(int j = 1;j <= len2;j++)
            {
                if(a[i - 1] == b[j - 1])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]);
            }
        }
        //if(dp[len1][len2] > 1)
            cout << "Case #" << ++num << ": you can visit at most " << dp[len1][len2] << " cities." << endl;
        /*else
            cout << "Case #" << ++num << ": you can visit at most " << dp[len1][len2] << " city." << endl;*/
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: