palindrome

topic background
IOI2000 Question 1
Topic description
A palindrome is a symmetrical string. Any given string can be turned into a palindrome by inserting several characters. The task of this problem is to find the minimum number of characters that need to be inserted to turn a given string into a palindrome.
For example, "Ab3bd" can become a palindrome after inserting 2 characters into "dAb3bAd" or "Adb3bdA", but inserting less than 2 characters cannot become a palindrome.
Note: This question is case sensitive
Input and output format
Input format:
a string ( 0 < strlen <= 1000 )
Output format:
There is exactly one integer, the minimum number of characters to be inserted
Input and output example
Input sample # 1 :
Ab3bd
Sample output # 1 :
 2
topic

untie:

The palindrome, that is, the result of forward reading and reverse reading is the same,

Let s1 be the original string and s2 be the reversed string

The title is transformed into adding the least characters to make the two strings of s1 and s2 equal

That is to find the longest common part, the rest is what the other party does not have, that is, to add

 

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define DB double
 4 using namespace std;
 5 const int N=1010;
 6 char s1[N],s2[N];
 7 int dp[N][N],len;
 8 int main()
 9 {
10     scanf("%s",s1+1);
11     for(int j=strlen(s1+1);j>=1;--j) s2[++len]=s1[j];
12     for(int i=1;i<=len;++i)
13      for(int j=1;j<=len;++j)
14       if(s1[i]==s2[j]) dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
15       else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
16     cout<<len-dp[len][len];
17     return 0;
18 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324536687&siteId=291194637