Title Link: http://poj.org/problem?id=1159
Palindrome
| Time Limit: 3000MS |
|
Memory Limit: 65536K |
| Total Submissions: 56628 |
|
Accepted: 19577 |
Description
A palindrome is a symmetrical string, which is, a string read identically from the left to the right as well as from the right to left. You-to-write a program which, given a string, determines the minimal number of characters to being inserted into the STR ing in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "ab3bd" can is transformed into a palindrome ("Dab3bad" or "Adb3bda") . However, inserting fewer than 2 characters does not produce a palindrome.
Input
Your program was to read from standard input. The first line contains one integer:the length of the input string n, 3 <= n <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from "a" to "Z ', lowercase letters from" a "to" Z ' and digits from ' 0 "to ' 9 '. Uppercase and lowercase letters is to be considered distinct.
Output
Your program is-to-write to standard output. The first line contains one integer and which is the desired minimal number.
Sample Input
5ab3bd
Sample Output
2
Source
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> using namespace std; #define LL long longconst double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 0x3f3f3f3f;co NST int MOD = 1000000007;int n;short int Dp[5005][5005];char str[5005];int main () { cin>>n; cin>>str; memset (DP, 0, sizeof (DP)); for (int i=1, i<n; i++) {for (int j=i-1; j>=0; j--) { if (str[i]! = str[j]) dp[i][j] = min (dp[i -1][J], dp[i][j+1]) +1; else dp[i][j] = dp[i-1][j+1]; } } cout<<dp[n-1][0]<<endl; return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ1159 (DP)