Palindrome

【题目链接】Palindrome

【题目类型】最长公共子序列

&题解:

你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在这个串中删除最少的字符,使得它回文是一样的.

那么我们可以把这个串reverse,之后的串称为s2,找s2和s的最长公共子序列就好了,因为有了LCS,接着把其他的都删掉,就是一个回文串了,因为正着读和倒着读都一样

还有POJ居然能跑5000^2 我的923MS就跑完了,还是很快的嘛,当然这题还可以滚动数组,要对下标取模什么的也许就可以了吧,我用的是short来减小内存

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 5000 + 7;
short n, dp[maxn][maxn];
char s1[maxn], s2[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("E:1.in", "r", stdin);
#endif
while(~scanf("%d", &n)) {
cle(dp, 0);
scanf("%s", s1);
strcpy(s2, s1);
reverse(s2, s2 + n);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= n; j++) {
// printf("%3d", dp[i][j]);
// }
// printf("\n");
// }
printf("%d\n", n - dp[n][n]);
}
return 0;
}

POJ 1159 Palindrome(最长公共子序列)的更多相关文章

  1. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. POJ 1159:Palindrome 最长公共子序列

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

  3. POJ1159——Palindrome(最长公共子序列+滚动数组)

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

  4. Palindrome(最长公共子序列)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48526   Accepted: 16674 Description A p ...

  5. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  6. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. 周赛F题 POJ 1458(最长公共子序列)

    F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Description ...

  8. hdu 1159求最长公共子序列

    题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...

  9. HDU 1159 LCS最长公共子序列

    #include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...

随机推荐

  1. Python3中的真值测试

    1. 真值测试 所谓真值测试,是指当一种类型对象出现在if或者while条件语句中时,对象值表现为True或者False.弄清楚各种情况下的真值对我们编写程序有重要的意义. 对于一个对象a,其真值定义 ...

  2. Object 中 equals()使用

    详细看: https://www.cnblogs.com/naihuangbao/p/9445027.html 1. ==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所 ...

  3. Do-Now—团队Scrum 冲刺博客五

    各个成员今日完成的任务 侯泽洋: 每日任务页面编写,任务修改功能 周亚杰:完成个人中心页面设计 王志伟:完成个人中心页面设计 唐才铭:启动动画及引导页与项目合并 项目燃尽图 站立式会议照片 各个成员遇 ...

  4. python学习笔记1-python相关应用套件

    完整的数据分析套件 统计科学计算 Numpy,Scipy,statsmodels 深度学习 TensorFlow,MXNET 结构化数据处理与分析 Pandas 大数据处理 PySpark 数据探索编 ...

  5. Tarjan求割点(割顶) 割边(桥)

    割点的定义: 感性理解,所谓割点就是在无向连通图中去掉这个点和所有和这个点有关的边之后,原先连通的块就会相互分离变成至少两个分离的连通块的点. 举个例子: 图中的4号点就是割点,因为去掉4号点和有关边 ...

  6. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习4

    #include <iostream>using namespace std;const MAXSIZE=12;int main(){ char *month[MAXSIZE]={&quo ...

  7. Nested Dolls 贪心 + dp

    G: Nested Dolls Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 99     Solved: 19 Descript ...

  8. __x__(30)0908第五天__导航条的练习 <div>版本

    效果图:  html源代码: <!doctype html> <html> <head> <meta charset="utf-8" /& ...

  9. VSCode+Xdebug断点调试PHP(全攻略)

    一直都想把php断电调试记录下来,由于拖延症极其严重导致现在才写. 好了,刚去猛喝了几碗心灵鸡汤,趁着这股劲把"Visual Studio Code如何使用XDebug进行php断点调试&q ...

  10. wordpress chronus主题 显示文章阅读数

    wordpress chronus主题 显示文章阅读数 第一步:将下面的代码拷贝到文件 /wp-content/themes/chronus/inc/template-tags.php 中 funct ...