POJ1159:Palindrome(LCS小应用 回文)
地址: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小应用 回文)的更多相关文章
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- 【又见LCS】NYOJ-37 回文字符串
[题目链接] 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...
- Palindrome(最长回文串manacher算法)O(n)
Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 ...
- LeetCode 409. Longest Palindrome (最长回文)
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 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 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
随机推荐
- 隐马尔科夫模型(hidden Markov model, HMM)
- Generator生成器函数
接触过Ajax请求的会遇到过异步调用的问题,为了保证调用顺序的正确性,一般我们会在回调函数中调用,也有用到一些新的解决方案如Promise相关的技术. 在异步编程中,还有一种常用的解决方案,它就是Ge ...
- git fork同步是什么意思?
这篇文章主要介绍了git fork同步是什么意思?fork到了哪里?有什么用?怎样用?跟clone有什么差别?本文就一一解释这些问题,须要的朋友能够參考下 官方文档:http://help.githu ...
- TrustZone——开源库—Linaro—OP-TEE
想研究安全系统源代码的有福气了.曾经OVOS的代码缺少TA相关的实现. 这次的版本号,基本框架都有了.先看看架构图吧. 几家大公司做的,可能是ST牵头.页面有ST的LOGO. 代码质量较高. 未来也会 ...
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- string类(四、字符串比较相关)
string类比较相关: 1. string.Compare [static] 1/ string.Compare(string A, string B); 比较两个string,返回整数表示二者在排 ...
- 主线程不能执行耗时的操作,子线程不能更新Ui
在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 在看方法之前看一下Android中消息机制: 引用 Message:消息 ...
- 自定义View中的Path
我们用Path可以画返回图标,可以画搜索图标,也可以画一个圆,DIDI
- 《C#高级编程》学习笔记----c#内存管理--栈VS堆
本文转载自Netprawn,原文英文版地址 尽管在.net framework中我们不太需要关注内存管理和垃圾回收这方面的问题,但是出于提高我们应用程序性能的目的,在我们的脑子里还是需要有这方面的意识 ...
- windows安装oracle11g第二部
Oracle 11g数据库安装及配置 安装Oracle数据库: 1)压缩包解压,双击运行win64_11gR2_database\database\setup.exe 2)输入电子邮件,点击“下一步” ...