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. MVC个人网站开发笔记-150302

    上传图片 参考这篇文章:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 调用ajaxFileUpload,控制器里面编 ...

  2. android开发(27) 看看我的手机里都有什么传感器

    想看看我的HTC ONE x 具有什么传感器.写个代码RUN一下. 代码很简单,直接贴了 package zyf.demo.sensordemo; import java.util.List; imp ...

  3. css table第一列 宽度

    table{table-layout:fixed;}table tr td:first-child,table tr td:first-child{width:120px;} 首行第一个td定宽同列的 ...

  4. Intellij IDEA 将工程转换成maven工程 详解

    1> 右键工程,点击 Add Framework Support   2> 选中 Maven,再点击 OK   3> 工程根目录自动生成 pom.xml 文件,这样 工程就支持 Ma ...

  5. websocket 和 socket.io 之间的区别是什么

    socket.io封装了websocket,同时包含了其它的连接方式,比如Ajax.原因在于不是所有的浏览器都支持websocket,通过socket.io的封装,你不用关心里面用了什么连接方式.你在 ...

  6. oracle的启动和停用

    1.开始-运行-cmd-确定 2.cmd页面,输入set ORACLE_SID=(你的数据库实例名),回车,执行 3.继续输入‘sqlplus/nolog’,敲击回车键 4.sql输入栏,输入‘con ...

  7. json如何把键名作为变量?

    有时候在项目开发过程中,我们需要把json对象的键名作为一个变量.此时我们该怎么做呢? 传统的json数据格式如下: <script type="text/javascript&quo ...

  8. 为已经存在的本地项目添加git,以及从远程仓库拉取代码并切换远程分支

    前提:先去gitlab或github网站上创建一个新项目,完毕后记得添加.ignore: 1.打开终端​,cd到已存在项目的目录 ​​​2.输入以下命令行,初始化一个本地仓库: ​git init 3 ...

  9. JSP之静态include指令、动态Include指令

    (一)使用静态include指令 <%@ page language="java" contentType="text/html; charset=gb2312&q ...

  10. 性能优化系列四:Tomcat优化

    一.Tomcat工作原理 1. TCP的三次握手四次挥手 三次握手: 说明: 类比于A和B打电话: A对B说:你好,我是A,你能听到我说话吗? B对A说:嗯,我能听到你说话 A对B说:好,那我们开始聊 ...