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. hbase源码系列(四)数据模型-表定义和列族定义的具体含义

    hbase是一个KeyValue型的数据库,在<hbase实战>描述它的逻辑模型[行键,列族,列限定符,时间版本],物理模型是基于列族的.但实际情况是啥?还是上点代码吧. HTableDe ...

  2. Hbase 学习(七) rowkey设计

    一直以来对rowkey的设计都比较迷茫,<hbase权威指南>倒是给出了个还算靠谱的例子. 下面这个例子有点儿像帖子表结构,它的rowkey设计是这样的,可以简单的理解为,什么人在什么时间 ...

  3. 【javascript】js 检验密码强度

    最近一直在做通行证项目,里面的注册模块中输入密码需要显示密码强度(低中高).今天就把做的效果给大家分享下,代码没有网上搜索的那么复杂,能够满足一般的需求. html 代码如下: <!DOCTYP ...

  4. ApplicationContextAware接口的作用

      1/============== 在Web应用中,Spring容器通常采用声明式方式配置产生:开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器 ...

  5. jquery跨域访问解决方案

    客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...

  6. Javascript导航菜单13则

    来源:http://www.noupe.com/ajax/13-awesome-java-script-css-menu.html翻译:http://parandroid.com下面为你准备了13个利 ...

  7. Python __init__函数的使用

    class Cat: def __init__(self,_name): self.name = _name def eat(self): print("i am eating ." ...

  8. PHP CURL POST提交

    $_post_url = 'http://XXXXX/XXX'; $post = 'key=12&content_id='.$content_id.'&md5='.$storeStat ...

  9. python进行数据分析------相关分析

    相关分析 import statsmodels.api as sm import pandas as pd import numpy as np from patsy.highlevel import ...

  10. 【转】WPF查找子控件和父控件方法

    一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...