poj 1159 Palindrome
来源:互联网 发布:madness淘宝正品店 编辑:程序博客网 时间:2024/06/07 20:28
Palindrome
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 52446 Accepted: 18081
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
思路:::将输入的字符串 反转得到s2 求出s1 和 s2的最长公共子序列 用n-减去最长公共子序列的长度即可
注意开dp数组时用short类型 用int会超内存
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;#define maxn 5001char s1[maxn],s2[maxn];short dp[maxn][maxn];int lcs(int n,int m){int i,j;for(i=0;i<n;i++){for(j=0;j<m;j++){if(s1[i]==s2[j])dp[i+1][j+1]=dp[i][j]+1;elsedp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);}}return dp[n][m];}int main(){int n;int i;scanf("%d",&n);scanf("%s",s1);for(i=0;i<n;i++)s2[n-i-1]=s1[i];printf("%d\n",n-lcs(n,n));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
- Cocos2d-x类似coverflow效果
- Mac机上生成pem文件用于推送
- hdoj 2191 悼念512。。 【多重背包】+【二进制分解】
- MySQL索引背后的数据结构及算法原理
- 数组的初始化为 0 的学习
- poj 1159 Palindrome
- Map.keyset()
- Arduino 学习之路IV 求助
- 【DFS】poj 1088 滑雪
- 百炼(2755)dp
- 改进后的向量空间模型(VSM)
- 批处理梯度下降BGD与随机梯度下降SGD
- CentOS6.x升级MySQL版本5.1到5.6
- 使用NSSecureCoding协议进行对象编解码