hdu 1513

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

思路:正反分别求一次LCS,利用滚动数组对二取余滚动

 1 #include<stdio.h>
 2 #include<string.h> 
 3 #include<iostream>
 4 #include<math.h>
 5 using namespace std;
 6 
 7 char s1[5005],s2[5005];
 8 int dp[2][5005],n;
 9 
10 void lcs()
11 {
12     int i,j;
13     memset(dp,0,sizeof(dp));
14     for(i=1;i<=n;i++)
15     {
16         for(j=1;j<=n;j++)
17         {
18             int x=i%2;
19             int y=1-x;
20             if(s1[i-1] == s2[j-1])
21                 dp[x][j]=dp[y][j-1]+1;
22             else
23                 dp[x][j]=max(dp[y][j],dp[x][j-1]);
24         }
25     }
26 }
27 
28 int main()
29 {
30     int i,j;
31     while(~scanf("%d",&n))
32     {
33         scanf("%s",s1);
34         for(i=0;i<n;i++)
35         {
36             s2[i]=s1[n-1-i];
37         }
38         s2[i]='';
39         lcs();
40         printf("%d
",n-dp[n%2][n]);
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/pter/p/4876832.html