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

</pre></p><p><pre name="code" class="java">import java.util.Arrays;
import java.util.Scanner;

public class POJ1159_ieayoio {
	static String s;

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			int le = cin.nextInt();
			s = cin.next();
			short[][] f = new short[le + 5][le + 5];
			for (int i = 0; i < f.length; i++) {
				Arrays.fill(f[i], (short) 0);
			}
			for (int i = 1; i <= le; i++) {
				f[0][i] = (short) (le - i + 1);
			}
			for (int i = 1; i <= le; i++) {
				f[i][le + 1] = (short) i;
			}
			for (int i = 1; i <= le; i++)
				for (int j = le; j >= i; j--) {
					if (s(i) == s(j))
						f[i][j] = f[i - 1][j + 1];
					else {
						f[i][j] = (short) Math.min(f[i - 1][j] + 1,
								f[i][j + 1] + 1);
					}
				}
			int min = Integer.MAX_VALUE;
			for (int i = 1; i <= le - 1; i++) {
				if (min > f[i][i + 1])
					min = f[i][i + 1];
			}
			for (int i = 1; i <= le; i++) {
				if (min > f[i][i])
					min = f[i][i];
			}
			System.out.println(min);

		}
	}

	static char s(int i) {
		return s.charAt(i - 1);
	}
}

题目大意:求一个字符串最少添加几个字符可以变成回文串

我的思路就是f[i][j]表示左端数第i个字符,左端数在i右端的第j个字符,不过我开始只是觉得和最长公共子序列很像,没想到另一种方法就是求这个字符串和它逆串的最长公共子序列,然后再用总长度减去最长公共子序列的长度即为所求

POJ1159,Palindrome

时间: 2024-10-18 11:56:41

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

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

[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

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