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(最长公共子序列)的更多相关文章

  1. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  2. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  3. POJ 1159:Palindrome 最长公共子序列

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

  4. POJ1159——Palindrome(最长公共子序列+滚动数组)

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

  5. Palindrome(最长公共子序列)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48526   Accepted: 16674 Description A p ...

  6. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  7. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 周赛F题 POJ 1458(最长公共子序列)

    F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Description ...

  9. hdu 1159求最长公共子序列

    题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...

  10. HDU 1159 LCS最长公共子序列

    #include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...

随机推荐

  1. EUI组件之ProgressBar

    一.ProgressBar常规使用 拖动一个progressbar到exml 代码中使用 /** * 主页场景 * @author chenkai 2018/5/26 */ class HomeSce ...

  2. 服务器报错 500,请确保 ASP.NET State Service(ASP.NET 状态服务)已启动

    报错信息: 解决方案: 开启此服务

  3. java的this表示当前类还是当前实例?

    转自:http://www.runoob.com/java/java-basic-syntax.html this 表示调用当前实例或者调用另一个构造函数

  4. DFS判断正环

    hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU_3486_Interviewe

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. oracle Bug 4287115(ora-12083)

    今天公司开发在删除表时报错ora-12083,很是疑惑,数据字典记录的是表,而删除要用物化视图方式删除,如下: SQL> DROP TABLE CODE_M_AGENCY;DROP TABLE ...

  7. Day19 客户关系系统实战

    day19 今日内容 Service事务 客户关系管理系统     Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 ...

  8. go-003-基础语法

    1.行分隔符 一行代表一个语句结束. 如果一行多个,使用“;”分割,不推荐使用,建议使用默认一行一个语句 2.标识符 标识符用来命名变量.类型等程序实体.一个标识符实际上就是一个或是多个字母(A~Z和 ...

  9. Laravel 5.* 中路由绑定 Controller 包含子目录写法

    https://blog.csdn.net/maxsky/article/details/54017981 [可以使用命令在项目根目录一键创建 php artisan make:controller ...

  10. Zabbix设置自定义监控

    [zabbix]自定义监控项key值   说明: zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,这里介绍一种自定义监控项的方式. 1,首先编 ...