标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3639 Accepted Submission(s): 1697
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 105; 4 char s[maxn], t[maxn]; 5 int dp[maxn][maxn]; 6 7 int main(){ 8 while (~scanf("%s%s", s, t)){ 9 memset(dp, 0, sizeof(dp)); 10 int len = strlen(s); 11 for (int j=0; j<len; ++j) 12 for (int i=j; i>=0; --i){ 13 dp[i][j] = dp[i+1][j]+1; // 每一个都单独刷 14 for (int k=i+1; k<=j; ++k) 15 if (t[i] == t[k]) // 区间内有相同颜色,考虑一起刷 16 dp[i][j] = min(dp[i][j], dp[i+1][k]+dp[k+1][j]); 17 } 18 for (int i=0; i<len; ++i){ 19 if (s[i] == t[i]){ // 对应位置相同,可以不刷 20 if (i) 21 dp[0][i] = dp[0][i-1]; 22 else 23 dp[0][i] = 0; 24 } 25 else 26 for (int j=0; j<i; ++j) // 寻找当前区间的最优解 27 dp[0][i] = min(dp[0][i], dp[0][j]+dp[j+1][i]); 28 } 29 printf("%d\n", dp[0][len-1]); 30 } 31 return 0; 32 }
标签:
原文地址:http://www.cnblogs.com/Silenceneo-xw/p/5940747.html