地址:http://poj.org/problem?id=1159

题目需求:

给你一个字符串,求最少添加多少字符可以使之构成回文串。

题目解析:

简单做法是直接对它和它的逆序串求最长公共子序列长度len。n-len即为所求。(n为原串长度)

即 : 最少补充的字母数 = 原序列的长度 —  原串和逆序的最长公共子串长度 ,  如果最长公共子串长度==原序列长度,则是回文,反之须补充的数目为n-lcs;

这样做的原因如下:

要求最少添加几个字符,我们可以先从原串中找到一个最长回文串,然后对于原串中不属于这个回文串的字符,在它关于回文串中心的对称位置添加一个相同字符即可。那么需要添加的字符数量即为n-最长回文串长度。

最长回文串可以看作是原串中前面和后面字符的一种匹配(每个后面的字符在前面找到一个符合位置要求的与它相同的字符)。这种的回文匹配和原串与逆序串的公共子序列是一一对应的(一个回文匹配对应一个公共子序列,反之亦然),而且两者所涉及到的原串中的字符数量是相等的,也就是最长公共子序列对应最长回文串。原因陈述完毕。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
typedef int ll;
#define N 1010
using namespace std;
char a[],b[];
short int c[][];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",a);
for(int i=n-; i>=; i--)
b[n--i]=a[i];
for(int i=; i<n; i++)
{
c[i][]=;
c[][i]=;
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(a[i-]==b[j-])
{
c[i][j]=c[i-][j-]+;
}
else
{
c[i][j]=max(c[i-][j],c[i][j-]);
}
}
}
printf("%d\n",n-c[n][n]); }
return ;
}

POJ1159:Palindrome(LCS小应用 回文)的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  3. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

    131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...

  4. 【又见LCS】NYOJ-37 回文字符串

    [题目链接] 回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...

  5. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. LeetCode OJ:Palindrome Linked List(回文链表判断)

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  7. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. LeetCode 234. Palindrome Linked List (回文链表)

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  9. LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

    翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...

随机推荐

  1. JavaScript实现网页安全登录(转)

    现在很多商业网站的用户登录都是明码传输的,而一般用户又习惯于所有帐号使用相同的密码来保存,甚至很多人使用的密码和自己的银行帐号都一样哦!所 以嘛还是有一定的安全隐患的,YAHOO的免费邮箱登录使用了M ...

  2. git 停止在12% writing objects 怎么回事?

    git 停止在12% writing objects 怎么回事? 输入以下代码试一下: git config --global http.postBuffer 524288000  

  3. 第二百四十五节,Bootstrap标签页和工具提示插件

    Bootstrap标签页和工具提示插件 学习要点: 1.标签页 2.工具提示 本节课我们主要学习一下 Bootstrap 中的标签页和工具提示插件. 一.标签页选项卡 标签页也就是通常所说的选项卡功能 ...

  4. 【BZOJ】1004: [HNOI2008]Cards(置换群+polya+burnside)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1004 学习了下polya计数和burnside引理,最好的资料就是:<Pólya 计数法的应用 ...

  5. 【NOIP模拟题】Permutation(dp+高精度)

    首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...

  6. 微软ASP.NET网站部署指南(8):部署Code-Only更新

    1.  综述 初始化部署以后,你须要继续维护和更新你的网站.本章节将向你展示一个不包含数据库改变的部署升级流程.(下一章节将展示数据库改变的部署升级流程.) 提醒:假设依据本章节所做的操作出现错误信息 ...

  7. erlang的进程池。

    转自: http://blog.sina.com.cn/s/blog_96b8a1540101542m.html 主要组成部分: https://github.com/devinus/poolboy ...

  8. VS2008设置快捷键Ctrl+W关闭当前打开的文本编辑器窗口

    好多友好的软件关闭多标签页的当前页时都有Ctrl+W的快捷键,如Chrome浏览器,使用起来还是很方便的. 但是作为程序员,使用VS2008时有时会打开好多C++或C#源文件,需要关闭某个源文件时你需 ...

  9. 系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体

    系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 设计Role实体 p ...

  10. leetcode difficulty and frequency distribution chart

    Here is a difficulty and frequency distribution chart for each problem (which I got from the Interne ...