poj 1159 Palindrome_(最长公共子序列的应用)
来源:互联网 发布:java线程同步和异步 编辑:程序博客网 时间:2024/06/06 14:08
Palindrome
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 51934 Accepted: 17886
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 <iostream>#include <cstring>#include <algorithm>using namespace std;char str1[100010],str2[100010];int dp[2][5005];int main(){ int t,i,j; while(cin>>t) { memset(dp,0,sizeof(dp)); int l=t; for(i=0; i<t; i++) { cin>>str1[i]; str2[--l]=str1[i]; } for(i=0; i<t; i++) for(j=0; j<t; j++) { if(str1[i]==str2[j]) 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]); } cout<<t-dp[(t-1)%2][t-1]<<endl; } return 0;}
眨一看这道题,感觉摸不着头脑,然后看了别人的题解才恍然大悟这是一道最长公共子序列的应用,感觉题目挺好的!
一定要注意数组的空间优化,如果开一个5000*5000的二维数组内存会超限,不信的可以提交试试看看!
这道题要用滚动二维数组来优化,你道题你只需要用到数组的上一个横行,所以只要开一个2*5000的数组就ok了,然后取mod2的值就可以循环了!
0 0
- poj 1159 Palindrome_(最长公共子序列的应用)
- POJ 1159 最长公共子序列的应用
- poj 1159 Palindrome(最长公共子序列)
- poj 1159 Palindrome 最长公共子序列
- POJ 1159:Palindrome 最长公共子序列
- POJ - 1159 Palindrome 最长公共子序列
- 最长公共子序列的一个应用
- POJ 1159 Palindrome 最长公共子序列的问题
- poj 1458&poj 1159 最长公共子序列LCS
- 最长的公共子序列
- POJ 2774 两个字符串的最长公共子序列。
- POJ 1458 最长公共子序列的长度
- hdu 5795 最长公共子序列的应用-----公共子序列的个数
- POJ 1458/HDU 1159 最长公共子序列
- 【mark】最长公共子序列(poj 1458+hdu 1159)
- poj 1159 Palindrome(最长公共子序列 + 滚动数组)
- POJ--1159:Palindrome (DP求最长公共子序列)
- poj 1159 Palindrome 最长公共子序列 滚动数组
- 产品经理
- Linux中数据类型
- Explicit Constructors(显式构造函数)
- android 中Menu的使用
- Java IO:字节流与字符流
- poj 1159 Palindrome_(最长公共子序列的应用)
- Java--对数组排序的常见方法总结
- java工厂模式 多数据源
- PCL: Surface模块之GreedyProjectionTriangulation表面重建
- 一些iOS高效开源类库
- H264解码器源码(Android 1.6 版)
- EstimoteSDK-Android 文档翻译
- .net日期字符串的处理
- JSONObject 解析