链接:
Palindrome
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2430 Accepted Submission(s): 838
Problem Description
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order
to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Your program is 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 are to be considered distinct.
from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Sample Input
5
Ab3bd
Sample Output
2
Source
Recommend
linle
题意:
先给你字符串的长度 N
再给你 N 个字符组成的序列,问最小多少个操作【添加一些字符】可以将原来的序列变成回文串
分析:
就是逆序存一下当前序列,然后求出 LCS 【最长公共子序列】
用 N 减去所求的最长公共子序列就可以了
注意到序列很长 5000 如果用裸 dp[5000][5000] 就 MLE 了,
以前在 POJ 上做过这道题, 内存是 hdu 的两倍,看过一篇博客是关于记忆化存储的数组开 dp[maxn][maxn] short int 随便就搞过去了,然后前天晚上就一直 MLE 啊Orz
完了学妹对我说 %2 开个 dp[2][5000] 就可以过了。。。
也就是前一篇博客的滚动数组思想:
hdu 1159 Common Subsequence 【LCS 基础入门】
当前这一行的状态只由上一行的状态和这一行前面的状态确定,所以只有记录两组Orz
也就是说直接把 裸的 LCS 的数组定义改成 dp[2][maxn], 然后把裸的模板中的 i 全部 %2 就好了
最后输出 n - dp[n%2][n]
关于滚动数组 LCS 如果有不了解的自己去上一篇博客看
hdu 1159 Common Subsequence 【LCS 基础入门】
code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 5005;
int dp[2][maxn];
char s1[maxn], s2[maxn]; void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
scanf("%s", s1);
for(int i = 0; i < n; i++)
s2[i] = s1[n-i-1];
memset(dp,0,sizeof(dp));
LCS(n,n);
printf("%d\n", n-dp[n%2][n]);
}
return 0;
}