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. zabbix修改和查看登录密码

    author:hendsen chen date : 2018-08-30  16:48:18 1,登陆zabbix的服务器,查看zabbix的登陆密码: [root@jason ~]# mysql ...

  2. Egret P2 ( 一) 掉落的小球

    实际效果(蛋疼这个gif制作软件,帧率太低....): 一 Egret和P2的坐标系 首先了解下P2和Egret的坐标系. 某人链接:http://blog.csdn.net/qilei2010/ar ...

  3. 【BZOJ1568】[JSOI2008]Blue Mary开公司 线段树

    [BZOJ1568][JSOI2008]Blue Mary开公司 Description Input 第一行 :一个整数N ,表示方案和询问的总数.  接下来N行,每行开头一个单词“Query”或“P ...

  4. PHP移除json数据最右侧的逗号!

    具体函数是:PHP rtrim() 函数 参考地址: http://www.w3school.com.cn/php/func_string_rtrim.asp 参考: <!DOCTYPE htm ...

  5. 日期提取函数EXTRACT

    EXTRACT extracts and returns the value of a specified datetime field from a datetime or interval exp ...

  6. asp.net SessionState模式的配置及使用

    由于项目dll文件变动比较频繁,而保存登陆的状态又保存在Session中,所以导致用户经常无故掉线(PS:dll变动的时候导致Session).有一种方法可以长期保存session,那就是sessio ...

  7. JS实现数字千位符格式化方法

    /** * [number_format 参数说明:] * @param {[type]} number [number:要格式化的数字] * @param {[type]} decimals [de ...

  8. (2.10)Mysql之SQL基础——约束及主键重复处理

    (2.10)Mysql之SQL基础——约束及主键重复处理 关键词:mysql约束,批量插入数据主键冲突 [1]查看索引: show index from table_name; [2]查看有约束的列: ...

  9. mysql 删除表

    删除表 DROP TABLE 表名;

  10. 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 The serverRuntime@appConcurrentRequestLimit setting is being exceeded.

    今天下午17点左右,博客园博客站点出现这样的错误信息: Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@a ...