hdu 1513 Palindrome(LCS)

Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3641    Accepted Submission(s): 1252

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

题目大意:给定一个字符串,要求加最少的字符使得新字符串成为一个回文串。

回文串,就是从头到尾和从尾到头看都是一样的。那么对于一个非回文串s1,我将其倒置成s2。然后求s1与s2的最长公共子序列,对于剩下的不是公共的x个字符,我只要再补上x 个字符就可以组成回文串了。

#include<stdio.h>
#include<string.h>
int maxi(int a,int b)
{
	if(a>b)return a;
	else return b;
}
char str1[5105],str2[5105];
int dp[2][5105];
int main()
{
	int n,i,j,k,l;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%s",str1);
		for(i=0;i<n;i++)
		str2[i]=str1[n-1-i];
	//	printf("%s\n",str2);
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			int x=i%2;
			int y=1-x;
			if(str1[i-1]==str2[j-1])
			dp[x][j]=dp[y][j-1]+1;
			else
			{
				dp[x][j]=maxi(dp[y][j],dp[x][j-1]);
			}
		}
		printf("%d\n",n-dp[n%2][n]);
	}
}

这个题目用了滚动数组,如果直接开5000*5000,会爆内存。其实这里只要用到dp[0]和dp[1]就够了,因为是可以循环的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 23:51:46

hdu 1513 Palindrome(LCS)的相关文章

HDU 1513 Palindrome(最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个最少的个数是多少? 一开始以为只是一个普通的DP题,但是按照我的想法敲出来之后怎么样都W了,无奈搜了解题报告,得知其实这个就是一个最长公共子序列问题,就是求这个字符串跟它的逆序的 字符串的最长公共子序列.因为杭电的题内存都要求在32M内存以内,所以很开心的敲出来才发现10^6的数组都开不了,所以只好

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-

HDU 1513 Palindrome【LCS】

题意:给出一个字符串s,问至少加入多少个字母让它变成回文串 解题思路:求出该字符串与该字符串翻转后的最长公共子序列的长度,再用该字符串的长度减去最长公共子序列的长度即为所求 反思:因为题目所给的n的范围为3<=n<=5000,所以dp[][]数组如果开到dp[5005][5005],会超内存,此时应该就用滚动数组来优化 滚动数组的详细介绍http://blog.csdn.net/niushuai666/article/details/6677982 Palindrome Time Limit:

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

最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij=yj.例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列. 考虑最长公共子序列问题如何分解成

[ACM] hdu 1260 Tickets (动态规划)

Tickets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Jesus, what a great movie! Thou

【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)

昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以就地利用mysql,最易行.方案四:使用方案三,但增加一个表以减少冗余,但代价新开一个表,并且每次查询都select join两个表. 研究了 求最长公共子串问题,顺便研究了字符串匹配 字符串匹配的Boyer-Moore算法http://www.ruanyifeng.com/blog/2013/05/boy

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys