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. Bash Shell (十一)

    [教程主题]:Bash Shell [课程录制]: 创E [主要内容] [1] Hello World! 几乎所有的讲解编程的书给读者的第一个例子都是 Hello World 程序,那么我们今天也就从 ...

  2. input 模糊搜索

    <html> <head> <title>test</title> <script type="text/javascript" ...

  3. 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案

    ————————安装最新ppa的wine———————— sudo add-apt-repository ppa:ubuntu-wine/ppa sudo apt-get update sudo ap ...

  4. iOS 实时音频采集与播放Audio Unit使用

    前言 在iOS中有很多方法可以进行音视频采集.如 AVCaptureDevice, AudioQueue以及Audio Unit.其中 Audio Unit是最底层的接口,它的优点是功能强大,延迟低; ...

  5. QVariant类及QVariant与自定义数据类型转换的方法

    这个类型相当于是Java里面的Object,它把绝大多数Qt提供的数据类型都封装起来,起到一个数据类型“擦除”的作用.比如我们的 table单元格可以是string,也可以是int,也可以是一个颜色值 ...

  6. Mac使用git/github小结

    介绍 git 版本控制系统 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 2. github是一个git项目托管网站 注册地 ...

  7. Linux之统计特定进程运行数量

    比如统计用户名为albert运行python的进程数目 ps -u albert | grep -c "python"

  8. selenium操作浏览器-窗口切换

    package seleniumLearn1; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openq ...

  9. Android WiFi 日志记录(ASSOC_REJECT)

    记录Android N关联拒绝之后的相关的log. 10-26 20:35:35.844 2215 2215 D wpa_supplicant: * bssid_hint=44:48:c1:d6:57 ...

  10. hostapd作为radius服务器

    使用hostapd作为radius服务器,用于企业wifi加密认证. 参考链接: http://www.cnblogs.com/claruarius/p/5902141.html 去网上下载hosta ...