题目:http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1133
水题,但有个坑点。。。
坑在循环判断条件里面,如果用scanf读入来做判断条件的话就会超时,如果用gets就没问题。。。
真不知道是为什么,以前一直听说scanf挺快的,比cin快多了,没想到这次tle是因为scanf。。。我再也不相信scanf了。。。以后有字符串读入我都用gets了。。。
#include<cstdio>
#include<cstring>
const int maxn = 110;
int max(int a, int b)
{
if (a > b)
return a;
return b;
}
int main()
{
char a[maxn], b[maxn], cnt = 1;
while (gets(a) && a[0] != '#')
{
gets(b);
int c[maxn][maxn] = {0};
int la = strlen (a), lb = strlen (b);
for (int i = 0; i < la; i++)
for (int j = 0; j < lb; j++)
if (a[i] == b[j])
c[i + 1][j + 1] = c[i][j] +1;
else
c[i + 1][j + 1] = max(c[i][j + 1], c[i + 1][j]);
printf("Case #%d: you can visit at most %d cities.\n", cnt++, c[la][lb]);
}
return 0;
}