hdu 1513(dp+滚动数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513

思路:n这么大,可以采用滚动数组,然后就是求原串和反串的LCS了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int dp[2][5555];
 8 char str1[5555],str2[5555];
 9 int n;
10 
11 int main()
12 {
13     while(~scanf("%d",&n)){
14         scanf("%s",str1);
15         strcpy(str2,str1);
16         reverse(str2,str2+n);
17         memset(dp,0,sizeof(dp));
18         for(int i=1;i<=n;i++){
19             for(int j=1;j<=n;j++){
20                 if(str1[i-1]==str2[j-1]){
21                     dp[i%2][j]=dp[(i-1)%2][j-1]+1;
22                 }else 
23                     dp[i%2][j]=max(dp[i%2][j-1],dp[(i-1)%2][j]);
24             }
25         }
26         printf("%d
",n-dp[n%2][n]);
27     }
28     return 0;
29 }
30 
31         
View Code
原文地址:https://www.cnblogs.com/wally/p/3314051.html