poj 1159 Palindrome -- 回文串,动态规划

Palindrome

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 59029   Accepted: 20505

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

题意是给你一个字符串,让你最少添加多少个字符使得字符串为一个回文串,参考了网上大神们的想法 首先把原始的串反转,然后求这两个串的最长公共子序列,

需要添加的字符=字符串的长度-公共字符串

感觉很好理解,然后写一下最长公共子序列就好了,简单的dp啊

还有中间需要注意的地方,由于题目要求的内存为65536K,要是使用int保存

dp[5005][5005],毫无疑问内存超限了,好多大神写的是滚动数组。本弱弱利用short来存储dp数组,险过啊!!!由于int保存为4个字节,而short只用2个字节来保存,你懂得。

//神奇的short,不会超内存,
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
short dp[5005][5005];
char s1[5005],s2[5005];
int main(){
	int n,i,j;
	int ans;
	while(~scanf("%d",&n)){
		scanf("%s",s1);
		int l = strlen(s1);
		for(i=0;i<l;i++){
			s2[l-i-1]=s1[i];
		}
		s2[l] = '\0';
		memset(dp,0,sizeof(dp));
		for(i=1;i<=l;i++){
			for(j=1;j<=l;j++){
				if(s1[i-1]==s2[j-1]){
					dp[i][j]=dp[i-1][j-1]+1;
				}
				else
					dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		printf("%d\n",l-dp[l][l]);
	}
	return 0;
}
时间: 2024-10-13 15:14:32

poj 1159 Palindrome -- 回文串,动态规划的相关文章

POJ 1159-Palindrome(dp_回文串+滚动数组)

Palindrome Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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 wr

HDU 1513[Palindrome] 回文串

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

POJ 1159 Palindrome(字符串变回文:LCS)

http://poj.org/problem?id=1159 题意: 给你一个字符串, 问你做少需要在该字符串中插入几个字符能是的它变成一个回文串. 分析: 首先把原字符串和它的逆串进行匹配, 找出最长公共子序列. 那么最长公共子序列的字符串肯定是一个回文串. 所以原串剩下的部分是不构成回文的. 我们只需要添加剩下部分的字符到对应位置, 原串自然就变成了一个回文. 所以本题的解为: n 减去 (原串与逆串的LCS长度). 令dp[i][j]==x表示串A的前i个字符与串B的前j个字符的子串的最长

POJ - 1159 Palindrome(回文变形)

d.求对字符串最少添加几个字符可变为回文串. s. 简单做法是直接对它和它的逆序串求最长公共子序列长度len.n-len即为所求.(n为原串长度) 这样做的原因如下: 要求最少添加几个字符,我们可以先从原串中找到一个最长回文串,然后对于原串中不属于这个回文串的字符,在它关于回文串中心的对称位置添加一个相同字符即可.那么需要添加的字符数量即为n-最长回文串长度. 最长回文串可以看作是原串中前面和后面字符的一种匹配(每个后面的字符在前面找到一个符合位置要求的与它相同的字符).这种的回文匹配和原串与逆

POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[

poj 1159 Palindrome - 动态规划

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

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

集训第五周动态规划 G题 回文串

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 t

[LeetCode] Palindrome Partitioning II 拆分回文串之二

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"