UVA - 10192 - Vacation (动态规划, LCS)

来源:互联网 发布:聚游网络首页 编辑:程序博客网 时间:2024/05/22 22:47

点击打开链接

稍微修改了上面的代码提交了,LCS部分完全没错,但第一次TLE,然后修改了一下输入WA了,最后参考别人的输入才AC了。蛋疼……

题目原文:

The end of input occurs when the first sequence starts with an "#"character (without the quotes)

第一次是:scanf("%s%s", a, b) && *a!='#'

第二次是:gets(a) && gets(b) && *a!='#'

第三次是:gets(a) && *a!='#'

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX_N = 110, MAX_M = 11, INF = 0x3f3f3f3f;int N1, N2;char a[MAX_N], b[MAX_N];int d[MAX_N][MAX_N];int LCS(char a[], char b[]){memset(d, 0, sizeof(d));for(int i = 0; i < N1; i++)for(int j = 0; j < N2; j++)if(a[i] == b[j])d[i+1][j+1] = d[i][j] + 1;elsed[i+1][j+1] = max(d[i][j+1], d[i+1][j]);return d[N1][N2];}int main(){//freopen("in.txt", "r", stdin);int t = 1;while(gets(a) && *a!='#'){gets(b);N1 = strlen(a); N2 = strlen(b);printf("Case #%d: you can visit at most %d cities.\n", t++, LCS(a, b));}return 0;}


0 0
原创粉丝点击