poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 53414 | Accepted: 18449 |
Description
be inserted into the string in order 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
letters from 'A' to 'Z', lowercase letters 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
Source
使用short的5000*5000的数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
short dp[5100][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[i][j] = dp[i-1][j-1]+1 ;
else
dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
}
printf("%d\n", n-dp[n][n]);
}
return 0;
}
使用滚动数组
滚动数组:使用两行数组,模拟大的二维数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[2][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n , k ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
k = 0 ;
for(i = 1 ; i <= n ; i++)
{
k = 1 - k ;
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[k][j] = dp[1-k][j-1]+1 ;
else
dp[k][j] = max( dp[1-k][j],dp[k][j-1] );
}
}
printf("%d\n", n-dp[k][n]);
}
return 0;
}
poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)的更多相关文章
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- hdu 1080 dp(最长公共子序列变形)
题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G - G ...
- hdu1503 最长公共子序列变形
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...
- HDU 1513 Palindrome(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- hdu1159 dp(最长公共子序列)
题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
随机推荐
- scrollTop如何实现click后页面过渡滚动到顶部
用JS操作,body元素的scrollTop var getTop = document.getElementById("get-top"); var head = documen ...
- if else elif 用法和区别
1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...
- Onenote代码高亮的实现方法
最终效果图 最终的效果图如下: VBA的编写参考 主要参考的是这篇博客中的思路:如何在Word中排出漂亮的代码 将VBA脚本复制到Word中并设置快捷键 Alt+F11 打开Word中的 VBS,将下 ...
- (原)iOS 用recursiveDescription打印View
今天要做一个搜索功能,用到UISearchBar 无奈背景太丑,就自定义了一个,首先用View私有方法打印一下searchBar的层次, 具体修改代码如下 for (UIView *view in _ ...
- 对linux中source,fork,exec的理解以及case的 使用
fork 使用 fork 方式运行 script 时, 就是让 shell(parent process) 产生一个 child process 去执行该 script, 当 child proc ...
- C语言文件操作 FILE结构体
内存中的数据都是暂时的,当程序结束时,它们都将丢失.为了永久性的保存大量的数据,C语言提供了对文件的操作. 1.文件和流 C将每个文件简单地作为顺序字节流(如下图).每个文件用文件结束符结束,或者在特 ...
- python基础学习笔记——初识函数
什么是函数 我们目前为止,已经可以完成一些软件的基本功能了,那么我们来完成这样一个功能:约x 1 2 3 4 5 pint("拿出手机") print("打开陌陌&quo ...
- Python第三方库之openpyxl(7)
Python第三方库之openpyxl(7) 散点图 散点或xy图表类似于一些折线图.主要的区别在于,一个系列的值被绘制在另一个值上.当值未排序时,这是有用的. from openpyxl impor ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Python之Monitor监控线程(干货)
在日常工作中常遇到这样的情况,我们需要一个监控线程用于随时的获得其他进程的任务请求,或者我们需要监视某些资源等的变化,一个高效的Monitor程序如何使用python语言实现呢?为了解决上述问题,我将 ...