POJ 1159 Palindrome
2015-08-27 14:06
169 查看
Palindrome
DescriptionA 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.
InputYour 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.
OutputYour program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Sample Input
Sample Output
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 56756 | Accepted: 19631 |
InputYour 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.
OutputYour 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 题目大意:输出把当前字符串转换成回文串所要添加字符的最少字符 思路:最长公共子序列 普通的求最长公共子序列的方法会爆内存。滚动数组一发。
/* *********************************************** Author :PK28 Created Time :2015/8/27 13:44:52 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 5000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } int dp[2][maxn]; char s[maxn]; char t[maxn]; int main() { #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n; while(cin>>n){ cin>>s; strcpy(t,s); reverse(t,t+n); cle(dp); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(s[i-1]==t[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] ); } return 0; }
相关文章推荐
- leetcode 264 Ugly Number II(难易度:Medium)
- C/C++的就业,发展方向-个人而言
- 正则表达式入门教程
- Qt 对话框显示控制按钮
- cisco 设备配置ACL访问控制列表
- 获取自定义Calendar中的数据
- spring(13) spring整合hibernate
- 8.5.5 Bulk Data Loading for InnoDB Tables 批量数据加载
- Android 之窗口小部件详解
- 国内量化交易平台
- C#中实现文本框的滚动条自动滚到最底端
- 关于Nodejs的多进程模块Cluster
- 程序大作战,萌出一脸血!
- Linux cp 实现强行覆盖
- Unity+NGUI性能优化方法总结
- C# 工厂单例
- Nginx反向代理配置配置实例
- codeforces 343C Read Time 【二分】
- SQLiteManager 基本操作
- 黑马程序员——java基础 集合 (复习)