POJ 1159 Palindrome(最长公共子序列)
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(最长公共子序列)的更多相关文章
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ 1159:Palindrome 最长公共子序列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56273 Accepted: 19455 Desc ...
- POJ1159——Palindrome(最长公共子序列+滚动数组)
Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...
- Palindrome(最长公共子序列)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 48526 Accepted: 16674 Description A p ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 周赛F题 POJ 1458(最长公共子序列)
F - F Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description ...
- hdu 1159求最长公共子序列
题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...
- HDU 1159 LCS最长公共子序列
#include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...
随机推荐
- Linux系统下安装JDK
注意:linux系统下使用mount命令挂载Windows系统下的共享文件,详情见我的另一篇“Linux系统挂载Windows系统下的共享文件” 一.安装jdk 1.进入usr目录cd /usr 2. ...
- Spring Boot 项目实战(五)集成 Dubbo
一.前言 上篇介绍了 Redis 的集成过程,可用于解决热点数据访问的性能问题.随着业务复杂度的提高,单体应用越来越庞大,就好比一个类的代码行数越来越多,分而治之,切成多个类应该是更好的解决方法,所以 ...
- 两个排序链表的合并(Easy)
问题来源:选自leetcode 21:合并两个有序链表 问题描述: 题目给定信息: 给定两个有序链表,把两个链表合并成一个链表,并且合并后的链表依然是有序的.这两个链表中允许有重复元素 问题分析: 设 ...
- helm-chart5,模板和访问文件
提供的一些声明和使用命名模板段的操作: define在模板中声明一个新的命名模板 template导入一个命名模板 block 声明了一种特殊的可填写模板区域 首先,模板名称是全局的.如果声明两个具有 ...
- node版本升级参考
https://www.cnblogs.com/sese/p/9557161.html
- 动态规划 hdu 1024
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Ubuntu16.04更新python3.5到python3.7
下载wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz 解压tar zxvf Python-3.7.1rc2.tgzcd ...
- Django数据库,在原有表中添加新字段
1.在你要添加新字段的app的 models.py 文件中添加需要新增的字段(这里新增的是dress字段): from django.db import models # Create your mo ...
- Confluo: Distributed Monitoring and Diagnosis Stack for High-speed Networks
https://rise.cs.berkeley.edu/blog/confluo-millisecond-level-queries-on-large-scale-streaming-data/ht ...
- oracle_dataGuard_11G
[李红]--切记_从库只安装实例_不需要 dbca 创建数据库 但是 netca 创建监听看个人喜欢,我反正是创建了.[DataGuard_主数据库的参数配置]1.启用 force logging 功 ...