地址: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. php -- 反射ReflectionClass

    反射类:ReflectionClass 反射就是将其他类的结构给反应出来,从而可以对类的结构进行了解便于对类的使用. ReflectionClass::export(类名); 返回结果为三个数组:常量 ...

  2. C# 将MSMQ消息转换成Json格式

    PS:主要就是一个配置文件和一个转换函数 配置文件app.config  之前要ADD reference -->   system.configuration & using.syst ...

  3. 学习:java代码检测

    转自:http://zh.wikipedia.org/wiki/%E4%BB%A3%E7%A0%81%E5%BC%82%E5%91%B3 对于Java开发语言,有些工具,比如Checkstyle.PM ...

  4. Android笔记——Activity中的回传数据案例(装备选择)

    1.创建程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. ArcGIS GP应用-GP模型创建-缓冲区分析

    一.参考资料 http://www.cnblogs.com/HPhone/archive/2012/11/05/2755974.html 二.关键步骤 1.缓冲分析数据源可以选择:点.面.线 2.构建 ...

  6. .Net CCNet C#6.0 自动化编译问题解决

    一.问题描述 由于C#6.0一些新的语法特性,导致先前部署的CCNet持续集成平台出现问题,无论是手动还是命令行均不能编译.   二.解决方案 1.下载BuildTools_Full.exe,地址:h ...

  7. tableview直接滚动至最后一行

    类似聊天界面,tableview应该直接显示在最后一行,并且不应该有滚动的出现. 在网上查了很久,直接滚动至最后一行很容易实现,有两种方法比较好. 1. 调用scrollToRowAtIndexPat ...

  8. px像素单位与IOS像素单位的换算

    本文转载至  http://blog.csdn.net/fanyuna/article/details/24032663 30px转成磅为单位=22磅=二号 磅=(像素/96)*72 =(30/96) ...

  9. jquery 判断ul下是否存在li

    $("ul").has("li").length > 0;$("ul > li").length > 0;$(" ...

  10. IOS 设置ios中DatePicker的日期为中文格式

    设置ios中DatePicker的日期为中文格式 1.在模拟器中的“设置”-“通用”-“多语言环境”-“语言”设置为“简体中文”, 2.“区域格式”设置为“中国”.