POJ 1159
来源:互联网 发布:现在做淘宝 还行吗 编辑:程序博客网 时间:2024/06/07 16:06
Palindrome
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 41307 Accepted: 14052
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.
状态转移方程没啥说的,再加个滚动数组就好,纳闷的是人家怎么跑到0MS的
#include<stdio.h>#include<string.h>#include<iostream>#define clear(a,b) memset(a,b,sizeof(a))#define max(a,b) ((a)>(b)?(a):(b))#define maxn 5100int f[2][maxn];int LCS(char s1[],char s2[]){ int l = strlen(s1),i,j; clear(f,0); for(i = 1;i <= l;i++) for(j = 1;j <= l;j++) if (s1[i-1] == s2[j-1]) f[i&1][j] = f[(i-1)&1][j-1] + 1; else f[i&1][j] = max(f[i&1][j-1],f[(i-1)&1][j]); return f[l&1][l];}void work(){ char s[maxn],s1[maxn],s2[maxn]; int i,j,n,n2; scanf("%d%s",&n,s); for(i = 0;i < n;i++) { s1[i] = s[i]; s2[i] = s[n - i - 1]; s2[n] = s1[n] = 0; } printf("%d\n",(n - LCS(s1,s2))); return ;}int main(){ work(); return 0;}
- POJ 1159
- poj 1159
- poj 1159
- POJ 1159
- poj 1159
- poj 1159
- poj 1159
- poj-1159
- POJ 1159
- POJ 1159
- POJ 1159
- POJ-1159
- POJ 1159
- poj 1159
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- POJ 1159解题报告
- poj 1159 Palindrome
- 黑马程序员_结构和类有什么区别?
- matlab r2012a下载及更新摘要
- Struts2框架教程笔记一
- 【Ubuntu手记】Ubuntu下配置Android开发环境(特别篇:如何克服被墙)
- 最吸引人的网络广告
- POJ 1159
- 存储过程分页2
- 身份证验证之简单校验
- 从查询结果中查询数据
- 黑马程序员_C#结构体定义的详解
- asp.net子父页面传值
- 问题求解
- 用Java访问SQLite
- 深入Java核心 Java内存分配原理精讲