hdoj 1513 Palindrome

Palindrome

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

Total Submission(s): 4194    Accepted Submission(s): 1427

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

倒转字符串,然后与原字符串求最长公共子序列,然后用长度减就行了;

但这个题目要求字符串为5000,所以二维数组肯定爆内存,所以用滚动数组,用dp[2][5500]即可!

#include<stdio.h>
#include<string.h>
#define N 5500
int dp[2][N];
char s1[N],s2[N];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		int len,i,j;
		getchar();
		scanf("%s",s1);
		len=strlen(s1);
		for(i=len-1,j=0;i>=0;i--)//字符串倒序
		{
			s2[i]=s1[j++];
		}
		for(i=1;i<=len;i++)
		  for(j=1;j<=len;j++)
		   {
		   	 int x=i%2;//在两行之间 循环进行
		   	 int y=1-x;
		   	 if(s1[i-1]==s2[j-1])
		   	 dp[x][j]=dp[y][j-1]+1;
		   	 else
		   	 dp[x][j]=max(dp[x][j-1],dp[y][j]);
		   }
		   printf("%d\n",len-dp[len%2][len]);
	}
	return 0;
} 

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

时间: 2024-11-05 13:32:53

hdoj 1513 Palindrome的相关文章

hdoj 1513 Palindrome 【LCS】+【滚动数组】

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

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 Palindrome

题目就是给一个字符串问最少插入多少个字符能让原字符串变为回文字符串. 算法: 用原串的长度减去原串与翻转后的串的最大公共字串的长度,就是所求答案. 1 //#define LOCAL 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstring> 6 using namespace std; 7 8 const int maxn = 5000 + 5

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

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

【HDOJ】1513 Palindrome

DP,MLE后改为滚动数组AC. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 5001 6 #define INF 0x3f3f3f3f 7 char str[MAXN]; 8 short dp[2][MAXN]; 9 10 11 int getmin(int x, int y) { 12 return x<y ? x:y; 13 } 14 15 in

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] 回文串

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题目大意:给一个字符串,问最少加多少个字母能成为回文串. 关键思想:要解决的是回文子序列问题而不是回文子串.回文子序列怎么求?可以把字符串倒转一下,再求他们的最长公共子序列啊!想一想为什么.求出LCS长度之后,n-LCS才是我们要的答案,因为对于不是回文子序列的其他字符来说,都需要给他们一个对象才能回文(对称).还有一个问题是我们开不了5000*5000的数组,但观察到递推方程是只与两个状态有

HDU 1513 Palindrome【LCS】

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

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