POJ 1159 Palindrome(最长公共子序列)
http://poj.org/problem?id=1159
题意:
给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串。
思路:
计算出最长公共子序列,再用原长-LCS=所要添加的字符数。
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = + ; char s1[maxn], s2[maxn];
int n;
int d[][maxn]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d", &n))
{
int e = ;
scanf("%s", s1+);
//逆串
for (int i = ; i <= n; i++)
s2[n - i + ] = s1[i];
memset(d, , sizeof(d));
for (int i = ; i <= n; i++)
{
e = - e;
for (int j = ; j <= n; j++)
{ if (s1[i] == s2[j])
d[e][j] = d[ - e][j - ] + ;
else
d[e][j] = max(d[ - e][j], d[e][j - ]);
}
}
cout << n - d[e][n] << endl;
}
}
POJ 1159 Palindrome(最长公共子序列)的更多相关文章
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ 1159:Palindrome 最长公共子序列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56273 Accepted: 19455 Desc ...
- POJ1159——Palindrome(最长公共子序列+滚动数组)
Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...
- Palindrome(最长公共子序列)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 48526 Accepted: 16674 Description A p ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 周赛F题 POJ 1458(最长公共子序列)
F - F Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description ...
- hdu 1159求最长公共子序列
题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...
- HDU 1159 LCS最长公共子序列
#include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...
随机推荐
- fluentValidation集成到autofac
废话不说直接上代码 // 首先实现ValidatorFactory public class DependencyResolverValidatorFactory : ValidatorFactory ...
- .net配置404错误页面
如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长. 下面的方面可以帮助你的网站远离让人头大的页面. 第一步:配置web.config 打开web.config, ...
- 导出Excel过程中遇到的问题java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
尝试实现java导出功能时遇到如下报错: org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.No ...
- 利用阿里云腾讯云正版KMS服务器端口转发
注意:以下内容仅供实验,请勿用于任何非法用途我们知道,阿里云和腾讯云在内网部署了KMS服务器,而且是正版的,那么,有没有办法使用公网的计算机直接或间接连接到这些KMS服务器呢,受代理服务器和跳板机配置 ...
- scrapy爬虫系列之一--scrapy的基本用法
功能点:scrapy基本使用 爬取网站:传智播客老师 完整代码:https://files.cnblogs.com/files/bookwed/first.zip 主要代码: ff.py # -*- ...
- Warm up---hdu4612(缩点,树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥最少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
- Portugal 2 1 minute has Pipansihuan Germany and USA tacit or kick the ball
C Luo assists last moment so that Portugal "back to life", but with just two games to allo ...
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...
- [py][mx]django实现根据城市和课程机构类别过滤
实现根据城市&课程机构过滤 实现点谁谁高亮,支持取交集. 直接上代码吧 本质上是过滤,多层过滤,取交集 def get(self, request): all_orgs = CourseOrg ...
- zookeeper集群-solrcloud集群
本文只写具体的搭建过程,具体原理请看官网文档.国内博客都是基本上都是通过tomcat搭建的solr,本文是通过内部集成的jetty容器搭建. 一.zookeeper集群搭建 1.安装JAVA环境,版本 ...