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

Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order
to obtain a palindrome. 



As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome. 

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters
from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

题意是要求给定的字符串添加多少个会变成回文字符串,实际上就是要比较该字符串和它的逆序字符串的最长公共子序列,而不是字串!!!(之前WA就WA在这里),再用长度-最长公共子序列的长度就是结果。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; short int dp[5002][5002];
char b[5002];
string b_reverse;
int num; void cal()
{
int i,j,max_value=0; memset(dp,0,sizeof(dp)); for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(b[i]==b_reverse[j])
{
dp[i+1][j+1]=dp[i][j]+1;
}
else
{
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
max_value=max(max_value,(int)dp[i+1][j+1]);
}
}
cout<<num-max_value<<endl;
} int main()
{
scanf("%d",&num);
scanf("%s",b); b_reverse=b;
reverse(b,b+num); cal(); return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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. POJ1159——Palindrome(最长公共子序列+滚动数组)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Linux下如何查找sqlnet.ora 和listener.ora 和tnsnames.ora 配置文件的目录

    1.首先切换到oracle 用户下 使用env 查看数据库配置文件信息 2.然后找到LD_LIBRARY_PATH=/home/opt/oracle/product/11.2.0.4/db_1 (配置 ...

  2. 011-PHP获取数组中的元素

    <?php $monthName = array( /*定义$monthName[1]到$monthName[12]*/ 1=>"January", "Feb ...

  3. js封装ajax

    //封装ajax function ajax(obj) { //创建xhr对象; var xhr = new XMLHttpRequest(); obj.method = obj.method.toU ...

  4. Django(七)模型:字段属性、字段选项(参数)

    一.模型类属性命名限制 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/db/models/ 1)不能是python的保留关键字. 2)不允许 ...

  5. Linux在实际中的应用

    各位童鞋们,你们是如何度过这周周末的呢?这周末的我在家学习学习再学习,然而学习到一半,公司领导突然给我打了个电话过来说有同事等会儿会去客户那部署无人值守安装系统服务,问我去不去学习下.我想我正在学Li ...

  6. 略坑的C#自动回收机制

    说起这个坑货,要说说折腾了好久的bug,项目对方需要在32位系统上使用,C#加载图像扔给C++处理再返回.所以想好了,C#这边加载图像开好内存扔给C++,各自开的内存各自释放. 所以,在32位系统上出 ...

  7. cf 785#

    23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...

  8. ES6与ES5的继承

    ES6 ES6中的类 类与ES5中的构造函数写法类似 区别在于属性要放入constructor中,静态方法和属性实列不会继承 <script> class Person{ height=& ...

  9. 151-PHP nl2br函数(二)

    <?php $str="h\nt\nm\nl"; //定义一个多处换行的字串 echo "未处理前的输出形式:<br />{$str}"; $ ...

  10. tomcat conf目录下server.xml详解

    一.   一个server.xml配置实例 1 <Server port="8005" shutdown="SHUTDOWN"> 2 <Lis ...