String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 58 Accepted Submission(s): 20
Problem Description
Given 3 strings A, B, C, find the longest string D which satisfy the following rules:
a) D is the subsequence of A
b) D is the subsequence of B
c) C is the substring of D
Substring here means a consecutive subsequnce.
You need to output the length of D.
a) D is the subsequence of A
b) D is the subsequence of B
c) C is the substring of D
Substring here means a consecutive subsequnce.
You need to output the length of D.
Input
The first line of the input contains an integer T(T = 20) which means the number of test cases.
For each test case, the first line only contains string A, the second line only contains string B, and the third only contains string C.
The length of each string will not exceed 1000, and string C should always be the subsequence of string A and string B.
All the letters in each string are in lowercase.
For each test case, the first line only contains string A, the second line only contains string B, and the third only contains string C.
The length of each string will not exceed 1000, and string C should always be the subsequence of string A and string B.
All the letters in each string are in lowercase.
Output
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
Sample Input
2 aaaaa aaaa aa abcdef acebdf cf
Sample Output
Case #1: 4 Case #2: 3HintFor test one, D is "aaaa", and for test two, D is "acf".
Source
Recommend
zhuyuanchen520
题意: 有三个字符串A, B, C。 求串D。
D是A, B的公共子序列。
C是D的子串。
思路: LCS(最长公共子序列)
dp[i][j] 表示 A的前i, B的前j的最长公共子序列。
dp1[i][j] 表示 A的后i, B的后j的最长公共子序列。
假设C在A, B中匹配的头尾位置是 ai, aj, bi, bj;
ans = max(ans, dp[ai - 1][bi - 1] + dp1[aj + 1][bj + 1] + strlen(C) );
暴力求出 C与A, B的所有匹配的 ai, aj, ai, bj。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 1000 + 50;
char a[V], b[V], c[V];
int dp[V][V], dp1[V][V], sta[V * 3][2], top;
int T, len1, len2, len3, ans;
void LCS() {
int i, j;
for(i = 0; a[i]; ++i)
for(j = 0; b[j]; ++j)
if(a[i] == b[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
for(i = len1 - 1; i >= 0; --i)
for(j = len2 - 1; j >= 0; --j)
if(a[i] == b[j])
dp1[i + 1][j + 1] = dp1[i + 2][j + 2] + 1;
else
dp1[i + 1][j + 1] = max(dp1[i + 2][j + 1], dp1[i + 1][j + 2]);
}
void f(char ch[], int len) {
int i, j, k;
for(i = 0; i < len; ++i) {
if(ch[i] != c[0])
continue;
for(j = i, k = 0; j < len && k < len3; ++j)
if(ch[j] == c[k])
k++;
if(k == len3) {
sta[top][0] = i;
sta[top++][1] = j - 1;
}
else
break;
}
}
int main() {
int i, j, k, d = 1;
scanf("%d", &T);
getchar();
while(T--) {
ans = top = 0;
memset(dp, 0, sizeof(dp));
memset(dp1, 0, sizeof(dp1));
scanf("%s %s %s", &a, &b, &c);
len1 = strlen(a);
len2 = strlen(b);
len3 = strlen(c);
LCS();
f(a, len1);
int top1 = top;
f(b, len2);
int top2 = top - top1;
for(i = 0; i < top1; ++i)
for(j = 0; j < top2; ++j)
ans = max(ans, dp[sta[i][0]][sta[j + top1][0]] + dp1[sta[i][1] + 2][sta[j + top1][1] + 2]);
printf("Case #%d: %d\n", d++, ans + len3);
}
}