poj 1159 Palindrome
来源:互联网 发布:数据库删除一行数据 编辑:程序博客网 时间:2024/05/17 04:05
Palindrome
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 47427 Accepted: 16253
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
#include <stdio.h>#include <string.h>#include <stdlib.h>/* dp,题目大意:添加尽可能少的字符,使字符串回文,求添加的字符的个数 * 采用的方法是动态规划加滚动数组 * 解题思路:将字符串反转,求正反两个字符串的最长公共子序列 * 所求结果即为:字符串长度 - 公共子序列字符的个数 * dp[5005][5005],显然已经超内存的限制,所以采用滚动数组 * */#define MAX(x, y) ((x) > (y) ? (x) : (y));char data1[5005];char data2[5005];int dp[2][5005];int main(){//freopen("in.txt", "r", stdin);int n;scanf("%d%*c", &n);scanf("%s", data1);int i, j;for(i = 0; i < n; ++ i){data2[i] = data1[n - 1 - i];}memset(dp, 0, sizeof(dp));//求最长公共子序列for(i = 1; i <= n; ++ i){for(j = 1; j <= n; ++ j){if(data1[i - 1] == data2[j - 1]){dp[i % 2][j] = dp[(i - 1) % 2][j - 1] + 1;}else{dp[i % 2][j] = MAX(dp[(i - 1) % 2][j], dp[i % 2][j - 1]);}}}printf("%d\n", n - dp[n % 2][n]);return 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
- 10个有用的第三方iOS开源库
- Linq to sql :查询句法
- 在VirtualBox下安装CentOS教程(截图版)
- Linux Bash Shell入门教程
- POJ1236_Network of Schools_强连通分量_Korasaju算法
- poj 1159 Palindrome
- 《初学cocos2d-x》- 动画CCAnimation(2)
- Redis String类型数据常用的16条命令总结
- hdu 1806
- mysql 触发器
- Linux教程:文件系统基础知识
- poj 3264
- Linux下密码输入问题
- Queue队列容器