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 ...
随机推荐
- Android定制争夺战 三大主流ROM横评
随着MIUI在广大“机油”们心目中位置的逐渐攀升,越来越多的厂商也相继推出了属于自己的定制Android ROM,想以此来抢占这一新兴市场,像点心OS.腾讯的Tita以及近期比较热门的百度云ROM等等 ...
- Android之从Browser中打开本地的应用程序&微信检测是否有对应app
在对应的应用程序的AndroidManifest.xml中配置: <activity android:name=".ui.TabHostActivity" android:w ...
- RecyclerView 缓存机制详解
一 前言 RecyclerView据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView.GridView.RecyclerView可以用来代替传 ...
- 【SPOJ】【1825】Free Tour 2
点分治 点分治的例题2(本题代码结果为TLE……) 强烈谴责卡时限QAQ,T了无数次啊无数次…… 不过在N次的静态查错中倒是加深了对点分治的理解……也算因祸得福吧(自我安慰一下) TLE后的改进:每棵 ...
- 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSAUX
ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSAUX 表空间 > 编辑 表空间: SYSAUX 1. 一般信息返 ...
- C++ 使用TinyXML解析XML文件
1.介绍 读取和设置xml配置文件是最常用的操作,TinyXML是一个开源的解析XML的C++解析库,能够在Windows或Linux中编译.这个解析库的模型通过解析XML文件,然后在内存中生成DOM ...
- 巧妙利用jQuery和PHP打造类似360安全卫士防火墙功能开关(类似iphone界面)效果
安全卫士防火墙开启关闭的开关,可以将此功能应用在产品功能的开启和关闭功能上. 准备工作为了更好的演示本例,我们需要一个数据表,记录需要的功能说明及开启状态,表结构如下: CREATE TABLE `p ...
- EasyUI-datagrid-自动合并单元格
1.目标 1.1表格初始化完成后,已经自动合并好需要合并的行: 1.2当点击字段排序后,重新进行合并: 2.实现 2.1 引入插件 /** * author ____′↘夏悸 * create dat ...
- 设计模式之十五:訪问者模式(Visitor Pattern)
訪问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式. 据<大话设计模式>中说算是最复杂也是最难以理解的一种模式了. 定义(源于GoF<De ...
- 如何通过js关闭微信浏览器页面
WeixinJSBridge.call('closeWindow'); jssdk wx.closeWindow(); WeixinJSBridge对象还提供了哪些功能: WeixinJSBridge ...