/*
* POJ_1159.cpp
*
* Created on: 2013年10月29日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = 5005;
char str1[maxn],str2[maxn];
int maxlen[2][maxn]; int main(){
int n;
while(cin >> n ){
memset(maxlen,0,sizeof(maxlen)); scanf("%s",str1); int i,j;
int len = strlen(str1);
for(i = 0 ; i < len ; ++i){
str2[i] = str1[n-1 -i];//str2为str1的逆序串
} int e = 0;
for(i = 0 ; i < n ; ++i){//求公共子序列的最大长度
e = 1- e;//这里使用了滚动数组,因为直接开maxlen[5000][5000]会内存溢出...
for(j = 0 ; j < n ; ++j){
if(str1[i] == str2[j]){
maxlen[e][j] = maxlen[1-e][j-1] + 1;
}else{
maxlen[e][j] = max(maxlen[1-e][j],maxlen[e][j-1]);
}
}
} cout<<n - maxlen[e][n-1]<<endl;
} return 0;
}

(字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)的更多相关文章

  1. 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串

    Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...

  2. 最长(大)回文串的查找(字符串中找出最长的回文串)PHP实现

    首先还是先解释一下什么是回文串:就是从左到右或者从右到左读,都是同样的字符串.比如:上海自来水来自海上,bob等等. 那么什么又是找出最长回文串呢? 例如:字符串abcdefedcfggggggfc, ...

  3. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

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

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

  5. 动态规划+滚动数组 -- POJ 1159 Palindrome

    给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...

  6. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  7. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  8. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  9. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

随机推荐

  1. Golang做的验证码(2)

    前面一篇文章介绍了2个用Golang做的验证码 http://www.cnblogs.com/ghj1976/p/3392847.html  这里再补充几个: 1.在GAE上使用的Google的验证码 ...

  2. IIS6到7,web.config的配置

    如果在IIS6中你的web.config中是以下配置: 这是在IIS6中我们习惯的经典模式的配置 < system.web>  " " " " &q ...

  3. HTML5_布局and音视频

    HTML5_布局and音视频 I.HTML5标签的改变1.文档声明HTML语法是不区分大小写的HTML5的DTD声明为:<!doctype html>确保浏览器能在HTML5的标准模式下进 ...

  4. 【DWT笔记】基于小波变换的降噪技术

    [DWT笔记]基于小波变换的降噪技术 一.前言 在现实生活和工作中,噪声无处不在,在许多领域中,如天文.医学图像和计算机视觉方面收集到的数据常常是含有噪声的.噪声可能来自获取数据的过程,也可能来自环境 ...

  5. c/c++ 数字转成字符串, 字符串转成数字

    c/c++ 数字转成字符串, 字符串转成数字 ------转帖 数字转字符串: 用C++的streanstream: #include <sstream> #Include <str ...

  6. phpcms的增删改查操作整理

    一.查 ①select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') /** * 执行sql查询 ...

  7. how to install flash

    Choice 1: Install  Flash from Repository: This is fairly simple and easy and should work from most p ...

  8. 安装 nodejs图像处理模块 sharp

    sudo npm install sharp 报错: ERROR: Please install libvips by running: brew install homebrew/science/v ...

  9. return View()

  10. IIS7 503错误 Service Unavailable

    把相应的Application Pools的process model的Identity属性设置成“LocalSystem”就OK了