HDU 4681 String 正反求最长公共子序列+枚举
2013-08-15 20:03
155 查看
点击打开链接
Total Submission(s): 189 Accepted Submission(s): 81
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
[align=left]Sample Input[/align]
2
aaaaa
aaaa
aa
abcdef
acebdf
cf
[align=left]Sample Output[/align]
Case #1: 4
Case #2: 3
Hint
For test one, D is "aaaa", and for test two, D is "acf".
[align=left]Source[/align]
2013 Multi-University Training Contest 8
[align=left]Recommend[/align]
zhuyuanchen520
题意是说给你三个串A,B,C让你求串D。要求D是A的字串,也是B的字串,C是D的连续字串,让你求D的最长长度。
先将A和B的最长公共子序列求出来,然后分别将A和B倒过来,在求其最长公共子序列。然后枚举C串分别在A串和B串中所有的位置,并将其起点和终点记录下来。最后枚举所有的起点和终点,求最长长度。
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 189 Accepted Submission(s): 81
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
[align=left]Sample Input[/align]
2
aaaaa
aaaa
aa
abcdef
acebdf
cf
[align=left]Sample Output[/align]
Case #1: 4
Case #2: 3
Hint
For test one, D is "aaaa", and for test two, D is "acf".
[align=left]Source[/align]
2013 Multi-University Training Contest 8
[align=left]Recommend[/align]
zhuyuanchen520
题意是说给你三个串A,B,C让你求串D。要求D是A的字串,也是B的字串,C是D的连续字串,让你求D的最长长度。
先将A和B的最长公共子序列求出来,然后分别将A和B倒过来,在求其最长公共子序列。然后枚举C串分别在A串和B串中所有的位置,并将其起点和终点记录下来。最后枚举所有的起点和终点,求最长长度。
#include<stdio.h> #include<string.h> #define max(a,b) (a>b?a:b) #define M 1007 int dp1[M][M],dp2[M][M]; char s1[M],s2[M],s3[M],st1[M],st2[M]; int z1[M][2],z2[M][2]; int len1,len2,len3; int main() { int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { scanf("%s %s %s",s1,s2,s3); len1=strlen(s1); len2=strlen(s2); len3=strlen(s3); memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); //dpq[][]求的是s1和s2的最大公共子序列 for(int i=1;i<=len1;i++) for(int j=1;j<=len2;j++) if(s1[i-1]==s2[j-1]) dp1[i][j]=dp1[i-1][j-1]+1; else dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1]); //st1是将s1字符串转过来 for(int i=0;i<len1;i++) st1[i]=s1[len1-i-1]; //st2是将s2字符串转过来 for(int i=0;i<len2;i++) st2[i]=s2[len2-i-1]; //求的是st1和st2的最长公共子序列 for(int i=1;i<=len1;i++) for(int j=1;j<=len2;j++) if(st1[i-1]==st2[j-1]) dp2[i][j]=dp2[i-1][j-1]+1; else dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1]); //将s3的起点和终点在s1中的所有情况记录下来 int n1=0,n2=0,k,j; for(int i=0;i<len1;i++) { if(s1[i]==s3[0]) { k=1; for(j=i+1;j<len1;j++) { if(s1[j]==s3[k]) k++; if(k==len3) break; } if(j!=len1) { z1[n1][0]=i; z1[n1++][1]=j; } } } //将s3的起点和终点在s2中的所有情况记录下来 for(int i=0;i<len2;i++) { if(s2[i]==s3[0]) { k=1; for(j=i+1;j<len2;j++) { if(s2[j]==s3[k]) k++; if(k==len3) break; } if(j!=len2) { z2[n2][0]=i; z2[n2++][1]=j; } } } //枚举所有情况,找出s4最大的长度 int ans=0; for(int i=0;i<n1;i++) for(int j=0;j<n2;j++) ans=max(ans,dp1[z1[i][0]][z2[j][0]]+dp2[len1-z1[i][1]-1][len2-z2[j][1]-1]);//应为从0开始的所以要减去1 printf("Case #%d: %d\n",cas,ans+len3); } return 0; }
相关文章推荐
- HDU 4681 String
- HDU-4681 String 枚举+DP
- HDU 4681 String(DP)
- HDU 4681 String 最长公共子序列
- HDU 4681 String(2013多校8 1006题 DP)
- 2013多校联合8 String (hdu 4681)
- HDU 4681 String(枚举 + LCS)
- HDU 4681 String 解题报告
- 简单dp-hdu-4681-String
- HDU 4681 String (dp, LCS | 多校8)
- HDU 4681 STRING dp+暴力。
- hdu 4681 String (LCS)
- hdu 4681 String
- HDU 4681 String (最长公共子序列)
- hdu 4681 String(转载)
- hdu 4681 String (动态规划-最长公共子序列)
- HDU 4681 String(DP 最长公共子系列)
- hdu 4681 string
- HDU 4681 String
- hdu 4681 String(dp)