poj 1156 Palindrome
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 51631 | Accepted: 17768 |
Description
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.
Input
from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
Output
Sample Input
5
Ab3bd
Sample Output
2
题意:
给你一串字符串,让你求最少增加几个字符,才干使得这个字符串是个回文串。
分析:
设a[i]是这个字符串,b[i]是这个字符串的逆序串。
那么a[i],b[i]的最长公共子序列就是所求的字符串里拥有的最大的回文串。
然后用总串长度减去最大的回文串长度即为所求。
求最长公共子序列:
1).递归式写成:
2).回溯输出最长公共子序列过程:
#include<stdio.h>
#include<iostream>
using namespace std;
#define Max 5001 char a[Max],b[Max];
short int dp[Max][Max]; // 用short int数组 int max(int x,int y)
{
return (x>y?x:y);
} int main ()
{ int n,i,j;
scanf("%d",&n);
getchar();
for(i=1,j=n;i<=n,j>=1;i++,j--)
{
scanf("%c",&a[i]);
b[j]=a[i];
} for(i=0;i<=n;i++)
{
dp[i][0]=0;
dp[0][i]=0;
} for(i=1;i<=n;i++) // 求最长公共子序列
{
for(j=1;j<=n;j++)
{
if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
} }
printf("%d\n",n-dp[n][n]); // 总串长度减去最长公共子序列(最大的回文串)长度
return 0;
}
poj 1156 Palindrome的更多相关文章
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- OpenJudge/Poj 1159 Palindrome
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- poj 1159 Palindrome - 动态规划
A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...
- poj 1159 Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 59094 Accepted: 20528 Desc ...
- POJ 1159 - Palindrome (LCS, 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 55018 Accepted: 19024 Desc ...
- poj 1159 Palindrome(dp)
题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...
随机推荐
- python文本 字符串对齐
python 字符串对齐 场景: 字符串对齐 python提供非常容易的方法,使得字符串对齐 >>> print("abc".center (30,'-')) ...
- [翻译] ClockView 时钟
ClockView 时钟 https://github.com/nacho4d/ClockView Overview ClockView is s simple class that will sim ...
- [翻译] SWTableViewCell
SWTableViewCell An easy-to-use UITableViewCell subclass that implements a swippable content view whi ...
- Andorid之使用GMail后台偷偷发送邮件(不要干坏事噢=。 =)
工具类: import java.util.Date; import java.util.Properties; import javax.activation.CommandMap; import ...
- Snail—Hibernate反向生成实体类及配置文件
今天学习了Hibernate的反向生成类文件 第一步.打开myeclipse中的database视图,找到对应的表,选中后右键单击. watermark/2/text/aHR0cDovL2Jsb2cu ...
- ubuntu下mongodb启动脚本
run-mongodb.sh #!/bin/bash mongod --dbpath /usr/local/mongodb/data1 --logpath /usr/local/mongodb/log ...
- SystemVerilog Event Scheduling Algorithm
While simulating System Verilog design and its test-bench including assertions, events has to be dyn ...
- 附6 hystrix metrics and monitor
一.基本方式 hystrix为每一个commandKey提供了计数器 二.实现流程 https://raw.githubusercontent.com/wiki/Netflix/Hystrix/ima ...
- Android 时间日期Widget 开发详解
桌面Widget其实就是一个显示一些信息的工具(现在也有人开发了一些有实际操作功能的widget.例如相机widget,可以直接桌面拍照).不过总的来说,widget主要功能就是显示一些信息.我们今天 ...
- go的基结构体如何使用派生结构体的方法
将派生类的方法声明为接口嵌入到基结构体中,派生结构体声明该接口为自身.