杭电1513--Palindrome(滚动数组+LCS)

Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4062    Accepted Submission(s): 1384

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.

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.

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

IOI 2000

Recommend

linle   |   We have carefully selected several similar problems for you:  1505 1506 1074 1003 1025

RE:怎么把一个字符串变为回文串? → → 反转后求LCS; 为了不爆内存用滚动数组,吐个槽: 这题WA到恶心。

   滚动数组:http://blog.csdn.net/insistgogo/article/details/8581215

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 char a[5050], b[5050];
 6 int dp[2][5050];
 7 int main()
 8 {
 9     int n;
10     while(~scanf("%d", &n))
11     {
12         scanf("%s", b);
13         strcpy(a, b);
14         strrev(b);
15         memset(dp, 0, sizeof(dp));           //+F+++U++++K++
16         for(int i = 1; i <= n; i++)
17             for(int j = 1; j <= n; j++)
18             {
19                 if(a[i-1] == b[j-1])
20                     dp[i%2][j] = dp[(i-1)%2][j-1] + 1;
21                 else
22                     dp[i%2][j] = max(dp[(i-1)%2][j], dp[i%2][j-1]);
23             }
24             printf("%d\n", n - dp[n%2][n]);
25     }
26     return 0;
27 } 
时间: 2024-08-27 12:22:34

杭电1513--Palindrome(滚动数组+LCS)的相关文章

POJ 1159 Palindrome(滚动数组)

链接:click here 题意: 给你一串字符串,让你求最少加入几个字符,才能使得这个字符串是个回文串. 思路: 设a[i]是这个字符串,b[i]是这个字符串的逆序串.那么a[i],b[i]的最长公共子序列就是所求的字符串里拥有的最大的回文串.然后用总串长减去最大的回文串长度即为所求.求最长公共子序列的公式为:dp[i][j]=max(dp[i-1] [j],dp[i][j-1])if(a[i]==b[i])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); 如果直接

hdoj 1513 Palindrome【LCS+滚动数组】

Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3918    Accepted Submission(s): 1340 Problem Description A palindrome is a symmetrical string, that is, a string read identically from

HDU 1513 &amp;&amp; POJ 1159 Palindrome (DP+LCS+滚动数组)

题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求LCS,这样公共的就求出来了,那么再用总数减掉这个LCS, 那么剩下的肯定就是没有配对的了,就得必须加上了. 思路有了,这里又出现一个问题,这个求LCS,要用的空间复杂度太大了,MLE了...有了思路,还是过不了, 这个题应该用滚动数组来做,我想想在求LCS时,第一维我们只用到了i-1和i,所以呢,

LCS(滚动数组) POJ 1159 Palindrome

题目传送门 1 /* 2 LCS裸题:长度减去最大相同长度就是要插入的个数 3 dp数组二维都开5000的话就会超内存,这里就用到了滚动数组, 4 因为在LCS的计算中,i的变化只相差1,所以可以通过对2取余来进行滚动:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <algorithm> 10 #include <string> 1

poj 1159 Palindrome lcs+滚动数组

点击打开链接题目链接 Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52910   Accepted: 18248 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

POJ 1159 - Palindrome (LCS, 滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 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

POJ 1159 Palindrome(lcs加滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52350   Accepted: 18041 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

hdu 1513 添最少字回文 (Lcs 滚动数组)

http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表示前一个的结果,不断滚动即可 #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cma

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-