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. iptables配置文件/etc/sysconfig/iptables内容详解

    #头两行是注释说明# Firewall configuration written by system-config-securitylevel# Manual customization of th ...

  2. PCL点云分割(1)

    点云分割是根据空间,几何和纹理等特征对点云进行划分,使得同一划分内的点云拥有相似的特征,点云的有效分割往往是许多应用的前提,例如逆向工作,CAD领域对零件的不同扫描表面进行分割,然后才能更好的进行空洞 ...

  3. 客户端通过HTTP协议与服务端交换数据

        客户端(包括浏览器)通过HTTP协议与服务端交换数据的描述 发起请求 header 键值对中的key大小写不敏感 Accept: application/json Content-Type: ...

  4. 5、QT分析之网络编程

    原文地址:http://blog.163.com/net_worm/blog/static/127702419201002842553382/ 首先对Windows下的网络编程总结一下: 如果是服务器 ...

  5. 实验二 C#程序设计 总结

    通过本次实验,我按照书上的例子,一个例子一个例子地写下来,前七点感觉和C语言差不多,除了语法稍稍不同外,大体上是一样的.到了第八点,对异常的处理,另我十分印象深刻.因为我做例3.21的时候,按照例子要 ...

  6. Android 8 声音调整过程

    记录Android 8声音调整过程. frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.j ...

  7. 信噪比——信号加噪相关的知识

    信噪比:即Signal noise ratio , 即SNR: 它的单位为 dB, 公式为: SNR = 10lg(PS / PN), 其中 ps 表示信号的有效功率, pn 表示噪声的有效功率: 如 ...

  8. 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理

    第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术.设置用户代理 如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执 ...

  9. 用不上索引的SQL语句

    下面介绍六种建立索引后不起作用的sql语句. 1.使用不等于操作符(<>, !=) SELECT * FROM dept WHERE staff_num <> 1000; × ...

  10. unity-------------------打包BuildAssetBundles的使用

    unity5打包机制下,一种资源打ab和资源管理的方案.1.打ab: 1.设置平台 2.清楚所有资源的assetbundlename: string[] abNameArr = AssetDataba ...