【HDU】4681 String(DP)

来源:互联网 发布:ios上python编辑器推荐 编辑:程序博客网 时间:2024/06/04 18:31

String

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.
 

 

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.
 

 

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: 3
Hint
For test one, D is "aaaa", and for test two, D is "acf".

首先枚举C 在A 和B 的起始位置,要使A 和B 的公共子序列尽量大,那么C 在A 和B 的占用长度肯定越少越好。所以就分成两个问题:
1. 对A,B 的任意起始位置,求出最近的包含C 的结束位置。先记录一个数组A[i][j](0  i < lenA; 0  j < 26) 保存在i 位置后跟i 最近的字符j 的距离。这个数组可以O(N2) 暴力枚举出来。然后对于每个起始位置i,按C从左往右寻找距离当前位置最近的相应字符即可。


2. 对于任意A,B 的起始位置,求出A,B 在起始位置之前的最长公共子序列,用同样的方法求出与起始位置相应的结束位置以后的最长公共子序列。只需定义dp[i][j] 代表A 的i 位置下B 的j 位置下的最长公共子序列长度。复杂度为O(lenA  lenB)。上面两个问题都预处理后每次查询为O(1)。所以总复杂度为O(N2).



#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;const int MAXN = 1010;char str1[MAXN],str2[MAXN],str3[MAXN];int dp1[MAXN][MAXN];int dp2[MAXN][MAXN];int dp[MAXN][MAXN];int dp3[MAXN][MAXN];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;    scanf("%d",&T);    int iCase = 0;    while(T--)    {        iCase++;        scanf("%s%s%s",str1,str2,str3);        int len1 = strlen(str1);        int len2 = strlen(str2);        int len3 = strlen(str3);        for(int i = 0; i <= len1;i++)            dp[i][0] = 0;        for(int i = 0;i <= len2;i++)            dp[0][i] = 0;        for(int i = 1;i <= len1;i++)            for(int j = 1;j <= len2;j++)            {                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);                if(str1[i-1] == str2[j-1])                    dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);            }        for(int i = 0; i <= len1;i++)            dp3[i][0] = 0;        for(int i = 0;i <= len2;i++)            dp3[0][i] = 0;        for(int i = 1;i <= len1;i++)            for(int j = 1;j <= len2;j++)            {                dp3[i][j] = max(dp3[i-1][j],dp3[i][j-1]);                if(str1[len1-i] == str2[len2-j])                    dp3[i][j] = max(dp3[i][j],dp3[i-1][j-1]+1);            }        for(int i = 1;i <= len3;i++)            dp1[0][i] = -1;        for(int i = 0;i <= len1;i++)            dp1[i][0] = i;        for(int i = 1;i <= len1;i++)            for(int j = 1;j <= len3;j++)            {                if(str1[i-1] == str3[j-1])                    dp1[i][j] = dp1[i-1][j-1];                else dp1[i][j] = dp1[i-1][j];            }        for(int i = 1;i <= len3;i++)            dp2[0][i] = -1;        for(int i = 0;i <= len2;i++)            dp2[i][0] = i;        for(int i = 1;i <= len2;i++)            for(int j = 1;j <= len3;j++)            {                if(str2[i-1] == str3[j-1])                    dp2[i][j] = dp2[i-1][j-1];                else dp2[i][j] = dp2[i-1][j];            }        int ans = 0;        for(int i = 0;i <= len1;i++)            for(int j = 0;j <= len2;j++)            {                int t1 = dp1[len1-i][len3];                int t2 = dp2[len2-j][len3];                if(t1 == -1 || t2 == -1)continue;                ans = max(ans,dp3[i][j]+dp[t1][t2]);            }        printf("Case #%d: %d\n",iCase,ans+len3);    }    return 0;}

  1. #include<stdio.h>  
  2. #define MaxSize 50  
  3. typedef char ElemType;  
  4. struct List  
  5. {  
  6.  ElemType list[MaxSize];  
  7.  int size;  
  8. }  
  9. setnull(struct List *p)//置空  
  10. {  
  11.  p->size=0;  
  12. }  
  13. int length(struct List *p)//求长度  
  14. {  
  15.  return (p->size);  
  16. }  
  17. ElemType get(struct List *p,int i)//取第i歌节点  
  18. {  
  19.  if(i<1&&i>p->size)  
  20.   printf("位置参数error!/n");  
  21.  else  
  22.   return(p->list[i-1]);  
  23. }  
  24. int locate(struct List *p,ElemType x)//定位  
  25. {  
  26.  int i=0;  
  27.  while(i<p->size&&p->list[i]!=x) i++;  
  28.  if(i==p->size)  
  29.   return(-1);  
  30.  else  
  31.   return(i+1);  
  32. }  
  33. void insert(struct List *p,ElemType x,int i)//插入结点  
  34. {  
  35.  int j;  
  36.  if(i<1||i>p->size+1)  
  37.   printf("位置参数error!/n");  
  38.  else  
  39.  {  
  40.   p->size++;  
  41.   for(j=p->size-1;j>=i;j--)  
  42.    p->list[j]=p->list[j-1];  
  43.   p->list[j]=x;  
  44.  }  
  45. }  
  46. void del(struct List *p,int i)  
  47. {  
  48.  int j;  
  49.  if(i<1&&i>p->size+1)  
  50.   printf("位置参数error!/n");  
  51.  else  
  52.  {  
  53.   for(j=i-1;j<p->size-1;j++)//结点前移  
  54.    p->list[j]=p->list[j+1];  
  55.   p->size--;  
  56.  }  
  57. }  
  58. display(struct List *p)  
  59. {  
  60.  int j;  
  61.  if(p->size==0)  
  62.   printf("Empty rable!/n");  
  63.  else  
  64.  {  
  65.   printf("顺序表:/n");  
  66.   if(p->size==1)  
  67.    printf("%c",p->list[p->size]);  
  68.   else  
  69.   {  
  70.    for(j=0;j<p->size-1;j++)  
  71.     printf("%c->",p->list[j]);  
  72.    printf("%c",p->list[j]);  
  73.   }  
  74.   printf("/n");  
  75.  }  
  76. }  
  77. main()  
  78. {  
  79.  struct List L;  
  80.  setnull(&L);  
  81.  insert(&L,'a',1);  
  82.  insert(&L,'s',1);  
  83.  insert(&L,'f',2);  
  84.  insert(&L,'d',1);  
  85.  insert(&L,'z',3);  
  86.  insert(&L,'c',2);  
  87.  display(&L);  
  88.  printf("值%c位置:%5d/n:",'a',locate(&L,'a'));  
  89.  printf("delete the second element~/n");  
  90.  del(&L,2);  
  91.  display(&L);  
  92.  printf("delete the first element~/n");  
  93.  del(&L,1);  
  94.  display(&L);  
  95. }  


0 0