Palindrome(poj1159)(动态规划)

Palindrome

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 53877   Accepted: 18610

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

/*用short类型险过。
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
short dp[5002][5002];
int main()
{
	char a[5001],s[5001];
	int i,j,n;
	scanf("%d",&n);
	scanf("%s",a);
	memset(dp,0,sizeof(dp));
	for(i=0,j=n-1;i<n;i++,j--)
	{
		s[i]=a[j];
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(a[i-1]==s[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",n-dp[n][n]);
	return 0;
}
时间: 2024-12-18 00:27:07

Palindrome(poj1159)(动态规划)的相关文章

POJ1159 Palindrome 【动态规划】

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

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的. 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[i][j - 1]); 如果 i = j  dp[i][j] = 0; 14145138 10651 Pebble

POJ1159:动态规划

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

poj1159(动态规划或者lcs求最长字串)

http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=20480 LCS求最长公共子串 #include<stdio.h> #include<iostream> #include<map> #include<string.h> #include<vector> using namespace std; short a[5005][5005]; char s1[5005]; c

动态规划第五讲——leetcode上的题目动态规划汇总(上)

本节,我们将对leetcode上有关DP问题的题目做一个汇总和分析. 1.题目来源 Interleaving String 动态规划 二叉树 Unique Binary Search Trees 动态规划 二叉树 Word Break 动态规划 N/A Word Break II 动态规划 N/A Palindrome Partitioning 动态规划 N/A Palindrome Partitioning II 动态规划 N/A Triangle 动态规划 N/A Distinct Subs

leetcode解题目录

参考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 不过本文准备用超链接的方式连接到相应解答页面,不断更新中 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II BFS 哈希表 Surrounded Regions BFS 矩阵 Word Ladder BFS N/A Binary Tree Level Order Traversal BFS|前序遍历 队列 Binary Tre

区间DP基础篇之 POJ1159——Palindrome

题目大意:给定一个字符串,求最少插入几个字符让该字符串成为回文串 法一: dp[i][j]表示使区间[i,j]成为回文串最小插入的字符数,则状态转移方程 1.if s[i]==s[len-1] 则:d[i][j]=d[i+1][j-1] 2.else  d[i]=min(dp[i+1][j],dp[i][j-1]) 首尾字符不同的时候,有两种决策. 1.将新字符插在首位,那么状态就变成了dp[i+1][j]了. 2.将新字符插在末尾,则状态就变成了dp[i][j-1]了 .比较两种决策哪种更优就

POJ1159——Palindrome(最长公共子序列+滚动数组)

Palindrome DescriptionA 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 inser

POJ1159,Palindrome

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