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. 冰蝎动态二进制加密WebShell特征分析

    概述 冰蝎一款新型加密网站管理客户端,在实际的渗透测试过程中有非常不错的效果,能绕过目前市场上的大部分WAF.探针设备.本文将通过在虚拟环境中使用冰蝎,通过wireshark抓取冰蝎通信流量,结合平时 ...

  2. 如何让Dev支持c++11特性

    1.点击工具选择编译选项 2.在编译时加入以下命令点击之后再将-std=c++11加入,点击确定就ok了

  3. 011.Delphi插件之QPlugins,延时加载服务

    这个DEMO是是把DLL插件的相关信息做成了一个配置文件,主程序加载这个配置文件,从而起到延时加载的作用 主程序代码如下 unit Frm_Main; interface uses Winapi.Wi ...

  4. .net 4.0 : Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.****'

    解决办法,添加 MicroSoft.CSharp 的引用.

  5. 响应式布局之 px、em、 rem

    一.写在前面的话 作为一面前端开发者,对 px .em . rem 应该是再熟悉不过了,但大多数小伙伴应该都和我一样仅仅停留在了解的层面,并不是实质性的掌握它们.本文对三者进行了详细的总结和详细说明, ...

  6. Golang的类型转换实战案例

    Golang的类型转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型概述 基础数据类型概述,博主推荐阅读: 布尔型: https://www.cnblogs. ...

  7. CANmonitor我自己编写的程序

    这个版本的程序, 上位机可以对电机的转速进行在线的设定,同时上位机接受电机控制器上报的母线电压,电机温度,控制器温度等. 在调试的过程中我遇见了一个问题,电机的转速的采样 . 根据协议:电机的转速为1 ...

  8. LINUX——LVM逻辑卷管理

    LVM: logical volumes manager LVM逻辑卷部署 物理卷—>卷组—>逻辑卷 第一步:关机添加磁盘:两个磁盘可以构成一个磁盘组. 第二步:查看磁盘 # ls /de ...

  9. x86平台inline hook原理和实现

    概念 inline hook是一种通过修改机器码的方式来实现hook的技术. 原理 对于正常执行的程序,它的函数调用流程大概是这样的: 0x1000地址的call指令执行后跳转到0x3000地址处执行 ...

  10. python面试题整理(二)

    1.进程,线程,协程定义,有什么区别 进程是操作系统分配资源的最小单位,一个进程对应一块CPU 线程是进程中的某一个控制单元,是CPU调度的最小单元,线程之间相互独立,进程结束线程也会结束,一个进程至 ...