UVA10100- Longest Match
2014-07-21 16:31
288 查看
题意:给你两串字符串,求最长的匹配数。
思路:LCS问题。但是在处理输入的数据时比较麻烦,而且要注意除了字母,数字也是其中所必需记录的。刚开始只存取了字母而忘了数字,所以WA了。
思路:LCS问题。但是在处理输入的数据时比较麻烦,而且要注意除了字母,数字也是其中所必需记录的。刚开始只存取了字母而忘了数字,所以WA了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1005; char s1[MAXN], s2[MAXN], str1[MAXN][MAXN], str2[MAXN][MAXN]; int d[MAXN][MAXN]; int dp(int l1, int l2) { for (int i = 1; i <= l1; i++) { for (int j = 1; j <= l2; j++) { if (strcmp(str1[i - 1], str2[j - 1]) == 0) d[i][j] = d[i - 1][j - 1] + 1; else d[i][j] = max(d[i - 1][j], d[i][j - 1]); } } return d[l1][l2]; } int main() { int t = 1; while (gets(s1) != NULL) { gets(s2); int l1 = strlen(s1); int l2 = strlen(s2); int num1 = 0, num2 = 0, cnt = 0; for (int i = 0; i <= l1; i++) { if (isalpha(s1[i]) || isdigit(s1[i])) { str1[num1][cnt++] = s1[i]; } else { if (isalpha(s1[i - 1]) || isdigit(s1[i - 1])) { str1[num1][cnt] = '\0'; num1++; cnt = 0; } } } cnt = 0; for (int i = 0; i <= l2; i++) { if (isalpha(s2[i]) || isdigit(s2[i])) { str2[num2][cnt++] = s2[i]; } else { if (isalpha(s2[i - 1]) || isdigit(s2[i - 1])) { str2[num2][cnt] = '\0'; num2++; cnt = 0; } } } memset(d, 0, sizeof(d)); int ans = dp(num1, num2); if (ans) printf("%2d. Length of longest match: %d\n", t++, ans); else printf("%2d. Blank!\n", t++); } return 0; }
相关文章推荐
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
- UVa 10100 - Longest Match
- UVA 10100 Longest Match
- UVA 10100 Longest Match
- UVa 10100 - Longest Match
- UVA 10100- Longest Match(dp之最长公共子序列)
- uva 10100 Longest Match(最长公共子序列)
- uva 10010 Longest Match
- SDUT 2080 最长公共子序列问题 (同uva10100 && POJ 1458 && HDU 1159)
- UVa 10071 - Back to High School Physics
- UVA10014 - Simple calculations
- uva 10891 - Game of Sum
- 【解题报告】uva103_Stacking Boxes(堆砌盒子, dp)
- POJ1505&amp;&amp;UVa714 Copying Books(DP)
- uva11396 dfs应用
- Uva - 839 - Not so Mobile
- Piotr's Ants(Uva 10881)
- Uva 156 Ananagrams
- uva340 模拟
- uva572 -- Oil Deposits