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



问你增加多少字符使得字符串变成回文串



我们对字符串和他的逆序串求一个最长公共子序列,这样一来。LCS里的一定是回文的,而其它的一定不会回文,所以必须在对应位置插入那些字符使得他们对称,最后整个串才干够回文

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; char str1[5010];
char str2[5010];
int dp[2][5010]; int main()
{
int len;
while (~scanf("%d", &len))
{
scanf("%s", str1);
for (int i = 0; i < len; i++)
{
str2[i] = str1[len - i - 1];
}
str2[len] = '\0';
memset (dp, 0, sizeof(dp) );
for (int i = 1; i <= len; i++)
{
for (int j = 1; j <= len; j++)
{
if (str1[i - 1] == str2[j - 1])
{
dp[i % 2][j] = dp[(i - 1) % 2][j - 1] + 1;
}
else
{
dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[i % 2][j - 1]);
}
}
}
printf("%d\n", len - dp[len % 2][len]);
}
}

POJ1159——Palindrome的更多相关文章

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

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

  2. poj1159 Palindrome

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  3. POJ1159 Palindrome(数位DP)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 58277   Accepted: 20221 Desc ...

  4. Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)

    一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...

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

    题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...

  6. POJ1159 - Palindrome(区间DP)

    题目大意 给定一个字符串S,问最少插入多少个字符可以使字符串S变为回文串 题解 用dp[i][j]表示把字符串s[i-j]变为回文串需要插入的最小字符数 如果s[i]==s[j]那么dp[i][j]= ...

  7. POJ1159 Palindrome(dp)

    题目链接. 分析: 感叹算法的力量. 方法一: 设 dp[i][j] 为字符串 s, 从 i 到 j 需要添加的最少字符数. 那么如果 s[i] == s[j], dp[i][j] = dp[i+1] ...

  8. POJ1159:Palindrome(LCS小应用 回文)

    地址:http://poj.org/problem?id=1159 题目需求: 给你一个字符串,求最少添加多少字符可以使之构成回文串. 题目解析: 简单做法是直接对它和它的逆序串求最长公共子序列长度l ...

  9. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

随机推荐

  1. [数据结构]迪杰斯特拉(Dijkstra)算法

    基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点vs(即从顶点vs开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点,而U则是记录还未求出最短路径的顶点(以及 ...

  2. Redis管道传输

    Redis是一个TCP服务器,并支持请求/响应协议.redis的一个请求完成需要下面的步骤: 客户端发送一个查询到服务器,并从套接字中读取,通常在封闭的方式,对服务器的响应. 服务器处理命令并将响应返 ...

  3. nodejs开发——require与exports的使用

    nodejs开发——require与exports的使用 另一片文章总结:http://www.cnblogs.com/hfultrastrong/p/8036682.html require req ...

  4. Matlab 读取excel文件提示服务器出现意外情况或无法读取问题解决

    1.问题描述: 该错误通常发生在应用函数读取excel文件(后缀xls或xlsx)时. 应用xlsread函数读取提示服务器出现意外情况: 应用importdata读取时提示can‘t open fi ...

  5. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  6. HBase二级索引与Join

    转自:http://www.oschina.net/question/12_32573 二级索引与索引Join是Online业务系统要求存储引擎提供的基本特性.RDBMS支持得比较好,NOSQL阵营也 ...

  7. 慎用 apt-get autoremove !

    apt-get 提供了一个用于下载和安装软件包的简易命令行界面. 卸载软件包主要有这3个命令 remove – 卸载软件包 autoremove – 卸载所有自动安装且不再使用的软件包 purge – ...

  8. linux sh 读取文件内容,if判读语句,变量var打印

    #!/bin/bash #1 读取文件 并显示 cat state.txt | while read line do echo $line done #2 读取文件 并显示 cat state.txt ...

  9. JAVA对于程序的运行的一种解说

    main方法,正如注释所说,这是程序开始执行的第一行.对于一个Java应用程序来说,main方法是必需的,Java解释器在没有生成任何对象的情况下,以main作为入口来执行程序.每个类中可以定义多个方 ...

  10. IntelliJ IDEA 2017 汉化包及教程

    一.准备 官网下载IntelliJ IDEA 2017 并安装好 下载汉化包 (百度云链接:http://pan.baidu.com/s/1slS9ZMP 密码:gp79) 二.汉化 此处有两种方法, ...