POJ 1159:Palindrome 最长公共子序列
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 56273 | Accepted: 19455 |
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
题意是要求给定的字符串添加多少个会变成回文字符串,实际上就是要比较该字符串和它的逆序字符串的最长公共子序列,而不是字串!!!(之前WA就WA在这里),再用长度-最长公共子序列的长度就是结果。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; short int dp[5002][5002];
char b[5002];
string b_reverse;
int num; void cal()
{
int i,j,max_value=0; memset(dp,0,sizeof(dp)); for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(b[i]==b_reverse[j])
{
dp[i+1][j+1]=dp[i][j]+1;
}
else
{
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
max_value=max(max_value,(int)dp[i+1][j+1]);
}
}
cout<<num-max_value<<endl;
} int main()
{
scanf("%d",&num);
scanf("%s",b); b_reverse=b;
reverse(b,b+num); cal(); return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1159:Palindrome 最长公共子序列的更多相关文章
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 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 ...
随机推荐
- 未知进程问题,process information unavailable
执行jps,有些未知进程: 2690 -- process information unavailable2666 原因:内存不足. cd /tmp/hsperfdata_impala/ 执行ll后, ...
- Day7 - J - Raising Modulo Numbers POJ - 1995
People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...
- PHP处理大数据量老用户头像更新的操作--解决数据量大超时的问题
/** * @title 老用户头像更新--每3秒调用一次接口,每次更新10条数据 * @example user/createHeadPicForOldUser? * @method GET * @ ...
- 011.Oracle数据库分页,取前10条数据
SELECT ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date( '2017-05-01', 'yyyy-MM-DD' ) AND to_dat ...
- 深度学习之常用linux命令总结
深度学习中常用linux命令总结 1.创建文件夹 mkdir 文件名2.删除文件 rm -d 目录名 #删除一个空目录 rmdir 目录名 #删除一个空目录 rm -r 目录名 #删除一个非空目录 r ...
- python中excel表格的读写
#!usr/bin/env python #-*- coding:utf-8 -*- import xlrd import xlwt from xlutils.copy import copy imp ...
- python爬虫笔记01
1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout ...
- PLC中双线圈问题
以上重要 .所以一个线圈的状态在一个扫描周期 只能刷新一次.
- Fedora-19安装texlive2013并配置中文
参考博文: http://blog.csdn.net/longerzone/article/details/8129124 之前通过yum install安装了texlive,不过在使用过程中老是报 ...
- ES6与ES5的继承
ES6 ES6中的类 类与ES5中的构造函数写法类似 区别在于属性要放入constructor中,静态方法和属性实列不会继承 <script> class Person{ height=& ...