hdu4632Palindrome subsequence
http://acm.hdu.edu.cn/showproblem.php?pid=4632
TLE了N次 原因居然是取模次数太多了。。! 这数据卡的好紧 还是我写的太搓。。828ms挤过
s[i]==s[j] dp[i][j] = dp[i][j-1]+dp[i+1][j]+1;
else dp[i][j] = dp[i][j-1]+d[[i+1][j]-dp[i+1][j-1];
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define mod 10007 7 char s[1010]; 8 int dp[1010][1010],k; 9 int find(int i,int j) 10 { 11 if(dp[i][j]) 12 return dp[i][j]; 13 if(i==j) 14 { 15 dp[i][j] = 1; 16 return 1; 17 } 18 if(j-i==1) 19 { 20 if(s[i]==s[j]) 21 { 22 dp[i][j] = 3; 23 return 3; 24 } 25 else 26 { 27 dp[i][j] = 2; 28 return 2; 29 } 30 } 31 if(s[i]==s[j]) 32 return dp[i][j]=(find(i,j-1)+find(i+1,j)+1)%mod; 33 else 34 return dp[i][j]=(find(i,j-1)+find(i+1,j)-find(i+1,j-1)+mod)%mod; 35 return dp[i][j]; 36 } 37 int main() 38 { 39 int i,j,t,kk=0; 40 scanf("%d",&t); 41 while(t--) 42 { 43 kk++; 44 memset(dp,0,sizeof(dp)); 45 scanf("%s",s); 46 k = strlen(s); 47 int ans = find(0,k-1); 48 printf("Case %d: ",kk); 49 printf("%d\n",ans); 50 } 51 return 0; 52 }