HDU 4681 STRING dp+暴力。
题意:不说了很好懂、
这题这么水= =。。。当时竟然没有勇气暴力搜一下。昨天(好吧前天。)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列。其实优化一下就是现在的答案啊~~!!!!给赵鹏说了赵鹏说不可能这么过。。然后就没想。就这样这么水的一道题都没过!!!!桑心啊~
代码:
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <vector> 5 using namespace std; 6 char sa[1005]; 7 char sb[1005]; 8 char sc[1005]; 9 int c[1005][1005]; 10 int d[1005][1005]; 11 struct node 12 { 13 int s,e; 14 }; 15 int main() 16 { 17 int t; 18 //freopen("in.txt","r",stdin); 19 // freopen("out1.txt","w",stdout); 20 int cas; 21 cas = 0; 22 scanf("%d",&t); 23 while(t--) 24 { 25 26 vector<node>vc,vd; 27 scanf("%s",sa+1); 28 scanf("%s",sb+1); 29 scanf("%s",sc+1); 30 31 int len1,len2,len3; 32 len1 = strlen(sa+1); 33 len2 = strlen(sb+1); 34 len3 = strlen(sc+1); 35 int i,j; 36 memset(c,0,sizeof(c)); 37 memset(d,0,sizeof(c)); 38 39 for(i = 1;i <= len1;i++) 40 { 41 for(j = 1; j <= len2; j++) 42 { 43 if(sa[i] == sb[j]) 44 c[i][j] = c[i-1][j-1]+1; 45 else 46 c[i][j] = max(c[i-1][j],c[i][j-1]); 47 } 48 } 49 50 for(i = len1;i>= 1;i--) 51 { 52 for(j = len2; j >= 1 ; j--) 53 { 54 if(sa[i] == sb[j]) 55 d[i][j] = d[i+1][j+1]+1; 56 else 57 d[i][j] = max(d[i+1][j],d[i][j+1]); 58 } 59 } 60 61 for(i = 1;i <= len1;i++) 62 { 63 int cnt; 64 cnt = 1; 65 if(sa[i] == sc[1]) 66 { 67 for(j = i;j <= len1;j++) 68 { 69 if(sa[j] == sc[cnt]) 70 cnt++; 71 if(cnt > len3) 72 { 73 struct node tmp; 74 tmp.s = i,tmp.e = j; 75 vc.push_back(tmp); 76 break; 77 } 78 } 79 } 80 } 81 for(i = 1;i <= len2;i++) 82 { 83 int cnt; 84 cnt = 1; 85 if(sb[i] == sc[1]) 86 { 87 for(j = i;j <= len2;j++) 88 { 89 if(sb[j] == sc[cnt]) 90 cnt++; 91 if(cnt > len3) 92 { 93 struct node tmp; 94 tmp.s = i;tmp.e = j; 95 vd.push_back(tmp); 96 break; 97 } 98 } 99 } 100 } 101 // printf("%d %d\n",c[len1][len2],d[1][1]); 102 int f,r; 103 int ans = 0; 104 for(i = 0;i < vc.size();i++) 105 { 106 for(j= 0;j < vd.size();j++) 107 { 108 if(vc[i].s == 1||vd[j].s == 1) 109 f = 0; 110 else f = c[vc[i].s-1][vd[j].s-1]; 111 if(vc[i].e == len1||vd[j].e == len2) 112 r = 0; 113 else 114 r = d[vc[i].e+1][vd[j].e+1]; 115 ans = max(ans,f+r+len3); 116 } 117 // printf("%d****\n",i); 118 } 119 120 printf("Case #%d: %d\n",++cas,ans); 121 //cout<<ans<<endl; 122 123 124 } 125 126 return 0; 127 }