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. 未知进程问题,process information unavailable

    执行jps,有些未知进程: 2690 -- process information unavailable2666 原因:内存不足. cd /tmp/hsperfdata_impala/ 执行ll后, ...

  2. Day7 - J - Raising Modulo Numbers POJ - 1995

    People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...

  3. PHP处理大数据量老用户头像更新的操作--解决数据量大超时的问题

    /** * @title 老用户头像更新--每3秒调用一次接口,每次更新10条数据 * @example user/createHeadPicForOldUser? * @method GET * @ ...

  4. 011.Oracle数据库分页,取前10条数据

    SELECT ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date( '2017-05-01', 'yyyy-MM-DD' ) AND to_dat ...

  5. 深度学习之常用linux命令总结

    深度学习中常用linux命令总结 1.创建文件夹 mkdir 文件名2.删除文件 rm -d 目录名 #删除一个空目录 rmdir 目录名 #删除一个空目录 rm -r 目录名 #删除一个非空目录 r ...

  6. python中excel表格的读写

    #!usr/bin/env python #-*- coding:utf-8 -*- import xlrd import xlwt from xlutils.copy import copy imp ...

  7. python爬虫笔记01

    1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout ...

  8. PLC中双线圈问题

    以上重要 .所以一个线圈的状态在一个扫描周期 只能刷新一次.

  9. Fedora-19安装texlive2013并配置中文

    参考博文:  http://blog.csdn.net/longerzone/article/details/8129124 之前通过yum install安装了texlive,不过在使用过程中老是报 ...

  10. ES6与ES5的继承

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