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. Windows驱动开发-r3和r0通信

    用户部分代码: int main() { HANDLE hDevice = CreateFile(L, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ...

  2. Spring Boot 核心注解与配置文件

    @SpringBootApplication注解 Spring Boot项目有一个入口类 (*Application) 在这个类中有一个main 方法,是运行该项目的切入点.而@SpringBootA ...

  3. Metasploit学习笔记——情报搜集技术(只记录与metasploit有关的)

    1.外围信息搜集 1.1whois域名注册信息查询 示例代码如下 msf > whois testfire.net 1.2网站的目录结构 示例代码如下 msf > use auxiliar ...

  4. 008.Oracle数据库 , 判断字段内容是否为空

    /*Oracle数据库查询日期在两者之间*/ SELECT PKID, OCCUR_DATE, ATA FROM LM_FAULT WHERE ( ( OCCUR_DATE >= to_date ...

  5. centos7创建ssh公钥

    步骤1:使用ssh-keygen命令创建公钥和私钥 [root@model /]# [root@model /]# ssh-keygen -t rsa -P '' Generating public/ ...

  6. maven项目打包和运行

    1. 添加pom <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</ar ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-eject

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. SDRAM调试总结

    SDRAM的调试总结 1 说明 实验平台: JZ2440 CPU: S3C2440 SDRAM型号: EM63A165TS-6G   2 SDRAM的一些基本概念 2.1 引脚分配   2.2 引脚描 ...

  9. P1037 在霍格沃茨找零钱

    转跳点:

  10. Ubuntu上安装tftp服务

    1. 安装 sudo apt install tftpd-hpa 2.设置工作目录 mkdir ~/tftpdroot tftpdroot 3.修改配置文件 sudo vi /etc/default/ ...