G - 回文串

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 

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

题解:回文串:要求得到一个字符串变为回文串要插入字符的最少数

输入a字符串的长度,再输入a字符串;

将a字符串复制到b中(相当于将a字符串反置)

求出其与a的最长公共子序列len;

则min=strlen(a)-len

Eg.

A:       Ad3db

B:       bd3dA

最长公共子序列为d3d

所以len=3;

Min=n-len=2

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
short d[][];
int dp(char *s, int n)
{
int i, j, k;
for (i=n-;i>=;i--)
{
for (j=i+;j<n;j++)
{
if (s[i] == s[j])
{
d[i][j] = d[i+][j-];
}
else
{
d[i][j] = min(d[i][j-], d[i+][j]) + ;
}
}
}
return d[][n-];
} int main( )
{
char str[];
int n;
while (scanf("%d", &n) ==)
{
scanf("%s",str);
printf("%d\n", dp(str, n));
}
return ;
}

poj1159 Palindrome的更多相关文章

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

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

  2. POJ1159 Palindrome(数位DP)

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

  3. POJ1159——Palindrome

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53647   Accepted: 18522 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. [Locked] One Edit Distance

    One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 分析: ...

  2. C++的一些内置函数

    C++里面有一些内置函数,实现了一些常用功能.虽然我手写也能写出这些函数,但是在srm或者其他一些需要速度的地方,用内置函数的优势就能体现出来了. 1.__gcd(a, b),返回a,b的最大公约数, ...

  3. 【最短路】血色先锋军(scarlet) 解题报告

    问题来源 BYVoid魔兽世界模拟赛 [问题描述] 巫妖王的天灾军团终于卷土重来,血色十字军组织了一支先锋军前往诺森德大陆对抗天灾军团,以及一切沾有亡灵气息的生物.孤立于联盟和部落的血色先锋军很快就遭 ...

  4. J - A Bug's Life - poj2492

    这个题目很有意思啊,有一些bug生物(肯定是程序员养的),有人观察他们的生活习惯,观察他们之间是否有同性恋关系,比如ab发生关系,bc发生关系,ab发生关系...产生了同性恋了,你需要判断一下这种关系 ...

  5. vue实现一个移动端屏蔽滑动的遮罩层

    在扯废话浪费大家的时间之前,先上个代码好了,使用vue实现起来很简单-- <div class="overlayer" @touchmove.stop > </d ...

  6. Swift-Dictionary

    1.字典写法 Dictionary<KeyType,ValueType>,KeyType是你想要储存的键,ValueType是你想要储存的值. 唯一的限制就是KeyType必须是可哈希的, ...

  7. Linux驱动程序开发 - 设备控制接口

    (2008-08-08 15:02:19) 转载▼ 标签: it linux kernel driver 分类: Linux 序言设备驱动程序的一个基本功能就是管理和控制设备,同时为用户应用程序提供管 ...

  8. Need a code of lazy load for div--reference

    1. For all DIVs of a page $(function() {  $("div").lazyload({effect: 'fadeIn'});}); 2. For ...

  9. 在浏览器地址栏按回车、F5、Ctrl+F5刷新网页的区别--转

    其中,在地址栏按回车又分为两种情况.一是请求的URI在浏览器缓存中未过期,此时,使用Firefox的firebug插件在浏览器里显示的HTTP请求消息头如下: Host    192.168.3.17 ...

  10. KVM与VMware的性能比较

    结合网页http://www.linuxidc.com/Linux/2011-01/31755.htm等整理 物理环境内存4G ,CPU4个,动态硬盘120G KVM: 硬盘测试瞬间读取速度Timin ...