Luo Gu P1435 palindrome string (LCS)

Portal


Problem-solving ideas

Obviously, this string upside down, and then the original to make a lcs.

Finally length -lcs length is the answer.

AC Code

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int n,dp[1005][1005];
 5 string s1,s2;
 6 int main()
 7 {
 8     cin>>s1;
 9     n=s1.length();
10     for(int i=n;i>=1;i--) s1[i]=s1[i-1];
11     for(int i=1;i<=n;i++) s2[i]=s1[n-i+1];
12     for(int i=1;i<=n;i++){
13         for(int j=1;j<=n;j++){
14             if(s1[i]==s2[j]){
15                 dp[i][j]=dp[i-1][j-1]+1;
16             }else{
17                 dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
18             }
19         }
20     }
21     cout<<n-dp[n][n];
22     return 0;
23 }

//IOI2000 day1t1

Guess you like

Origin www.cnblogs.com/yinyuqin/p/12131068.html