POJ 1159 Palindrome
来源:互联网 发布:宣传音频制作软件 编辑:程序博客网 时间:2024/06/13 19:10
Palindrome
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 62634 Accepted: 21827
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.
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
5Ab3bd
Sample Output
2
Source
IOI 2000
内存开销太大,会MLE所以改成了short int 类型 输入输出也用%d
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char s1[5005];char s2[5005];short int dp[5005][5005];int main(){ short int n; scanf("%d",&n); scanf("%s",s1); short int len=strlen(s1); short int i,j; for(i=0;i<=len-1;i++) { s2[i]=s1[len-1-i]; } for(i=0;i<=len;i++) { dp[i][0]=0; } for(i=0;i<=len;i++) { dp[0][i]=0; } for(i=1;i<=len;i++) { for(j=1;j<=len;j++) { if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } printf("%d\n",len-dp[len][len]); return 0;}
0 0
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 1159 Palindrome
- poj 1159 Palindrome
- dp poj 1159 Palindrome
- poj 1159 Palindrome
- poj 1159 Palindrome
- POJ 1159 Palindrome DP
- Palindrome poj 1159
- Poj 1159 Palindrome
- poj 1159 Palindrome
- poj 1159 Palindrome 【DP】
- poj 1159Palindrome
- poj 1159 Palindrome
- POJ 1159 Palindrome
- POJ 1159 Palindrome
- Poj 1159 Palindrome
- POJ 1159 Palindrome
- 商品展示案例
- 聪明的kk
- JavaScript 开发进阶:理解 JavaScript 作用域和作用域链
- python中类变量和实例变量__之实例变量
- 程序的链接+编译过程
- POJ 1159 Palindrome
- struts的拦截器
- 二、2.1 guava的集合类
- SVN的基本使用
- vs2015编译CEF
- HDU 2066 一个人的旅行(spfa,dijkstra)
- 料理记录……
- 使用list和tuple
- Java spi机制浅谈