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 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

题意:给定一个串,求最小加入几个字符能使其变成回文串。

题解:找出原串的逆串,再求n - LCS即得结果,原理是原串和逆串构成的公共序列必定是回文的,所以,对于那些不能匹配成回文的字符只需要在它与回文串中心对称的位置加上一个同样的字符即构成回文。

#include <stdio.h>
#include <string.h>
#define maxn 5002

char str1[maxn], str2[maxn];
short dp[maxn][maxn];

void getTraverse(int len)
{
	int i = 1;
	while(len) str2[i++] = str1[len--];
	str2[i] = '\0';
}

int max(short a, short b){
	return a > b ? a : b;
}

int LCS(int len)
{
	int i, j;
	memset(dp, 0, sizeof(dp));
	for(i = 1; i <= len; ++i){
		for(j = 1; j <= len; ++j)
			if(str1[i] == str2[j]) dp[i][j] = dp[i-1][j-1] + 1;
			else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
	}
	return dp[len][len];
}

int main()
{
	int n, len;
	while(scanf("%d", &n) == 1){
		scanf("%s", str1 + 1);
		len = strlen(str1 + 1);
		getTraverse(len);
		printf("%d\n", len - LCS(len));
	}
	return 0;
}

POJ1159 Palindrome 【动态规划】

时间: 2025-01-13 12:10:55

POJ1159 Palindrome 【动态规划】的相关文章

区间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 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

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: 52966   Accepted: 18271 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——Palindrome

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53647   Accepted: 18522 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 - 动态规划

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

[POJ1159]Palindrome(dp,滚动数组)

题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题卡内存真稀奇,于是修改成滚动数组.观察发现i值的更新只有可能是从i或i-1转移来,所以就i取模2. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏

滚动数组 (定义和用法)

滚动数组的作用在于优化空间,主要应用在递推或动态规划中(如01背包问题).因为DP题目是一个自底向上的扩展过程,我们常常需要用到的是连续的解,前面的解往往可以舍去.所以用滚动数组优化是很有效的.利用滚动数组的话在N很大的情况下可以达到压缩存储的作用. 一个简单的例子: 斐波那契数列: 一般代码: #include<iostream> #include<cstdio> using namespace std; int Fib[25]; int fib(int n) { Fib[0]

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim