博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1159 Palindrome(最长公共子序列)
阅读量:4707 次
发布时间:2019-06-10

本文共 1282 字,大约阅读时间需要 4 分钟。

Palindrome

【题目链接】

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

&题解:

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

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

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

&代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;}

转载于:https://www.cnblogs.com/s1124yy/p/7206159.html

你可能感兴趣的文章
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
OnePage收集
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>