P1435 palindrome string

Topic background

IOI2000 first question

Title Description

It is a symmetric palindromic word string. Any given string, by inserting a number of characters, words can become palindromic. Task this question is to obtain a given string becomes minimum number of characters required to insert the word palindrome.

For example, "Ab3bd" After inserting two characters can be turned into a palindrome word "dAb3bAd" or "Adb3bdA", but inserting characters can not become less than two palindromic word.

NOTE: This problem is case sensitive

Input Format

A string (0 <strlen <= 1000)

Output Format

There is one and only one integer, that is, the minimum number of characters to insert

Sample input and output

Input # 1
Ab3bd
Output # 1
2


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n,dp[5001][5001];
char str1[5001],str2[5001];
int main(){
    scanf("%s",str1+1);
    n=strlen(str1+1);
    for(int i=1;i<=n;i++){
        str2[i]=str1[n-i+1];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(str1[i]==str2[j]){
                dp[i][j]=dp[i-1][j-1]+1;
            }
            else{
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
    }
    printf("%d",n-dp[n][n]);
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/xiongchongwen/p/11261588.html