1513-Palindrome(LCS)

http://acm.hdu.edu.cn/showproblem.php?pid=1513

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 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         s2[i] = s1[n-1-i];
36         s2[i] = '';
37         LCS();
38         printf("%d
",n-dp[n%2][n]);
39     }
40 
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/wang-ya-wei/p/5543900.html