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 ...
随机推荐
- [oracle] update语句卡住问题
执行update语句的时候发现执行半天不成功 update main_order set order_source = '2', order_status = '2' 查询哪些对象被锁 select ...
- C语言错误: CRT detected that the application wrote to memory after end of heap buffer
CRT detected that the application wrote to memory after end of heap buffer 多是中间对其进行了一些操作,在程序结束处,释放内存 ...
- 异常之*** buffer overflow detected ***
*** buffer overflow detected *** 是sprintf()超出buff大小
- 【cf492】E. Vanya and Field(拓展欧几里得)
http://codeforces.com/contest/492/problem/E 一开始没时间想,,诶真是.. 挺水的一道题.. 将每个点的横坐标都转换成0,然后找纵坐标有多少即可..即解方程 ...
- HDU1717--小数化分数2
这道题是将输入的小数(有可能是无限循环小数)来化为分数.刚開始看到以为枚举(千万不要嘲笑我),可是感觉不正确. 所以百度了小数化为分数的方法,然后看到了各种方法,原来是这这样,在这我採用的是小数化为分 ...
- Hibernate_day04--HQL多表查询_Hibernate检索策略
HQL多表查询 Mysql里面多表查询 1 内连接 查的是两个表关联的数据 2 左外连接 查的是左边表所有数据,右边表关联数据 3 右外连接 HQL实现多表查询 Hql多表查询 (1)内连接 (2)左 ...
- 使用ProcDump工具抓取dump
首先得到要抓取的进程号 cd %windir%\syswow64\inetsrvappcmd list wp得到pid之后, 在任务管理器里发现w3wp.exe的CPU总在49%-60%左右, 间歇性 ...
- iOS 7 新特性:视图控制器切换API
本文转载至 http://blog.jobbole.com/51588/ 本文由 伯乐在线 - studentdeng 翻译自 Chris Eidhof.欢迎加入技术翻译小组.转载请参见文章末尾处的要 ...
- 基于java 的websocket的聊天功能,一开始初始化websocket,执行打开连接之后就直接关闭连接了。
1 错误描述: java 后台没有报错,但是就是连接不上,一连上又自动关闭. 2 错误根源: 缺少jar包. 对比了报错的tomcat 的jar包和不报错的jar包 发现是tomcat下缺少上图绿色框 ...
- 怎么在android实现通过浏览器点击链接打开apk
intent://scan/#Intent;scheme=appname://appname/[频道]/[id];package=com.appname.package;end http://m.ch ...