题目传送:UVA - 10192
思路:就是简单的最长公共子序列啦,不过输入居然还包含空格,然后很奇怪的TLE了,不是WA,心想n最大才100居然TLE,,好吧,可能有些数据特殊吧
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[105], s2[105]; int dp[105][105]; int cas; int main() { cas = 1; while(gets(s1 + 1), s1[1] != '#') { gets(s2 + 1); int len1 = strlen(s1 + 1), len2 = strlen(s2 + 1); memset(dp, 0, sizeof(dp)); for(int i = 1; i <= len1; i++) { for(int j = 1; j <= len2; 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]); } } printf("Case #%d: you can visit at most %d cities.\n", cas++, dp[len1][len2]); } return 0; }
时间: 2024-10-11 06:01:47