[POJ1159]Palindrome(dp,滚动数组)

题目链接:http://poj.org/problem?id=1159

题意:求一个字符串加多少个字符,可以变成一个回文串。
把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行。这题卡内存真稀奇,于是修改成滚动数组。观察发现i值的更新只有可能是从i或i-1转移来,所以就i取模2。

 1 /*
 2 ━━━━━┒ギリギリ♂ eye!
 3 ┓┏┓┏┓┃キリキリ♂ mind!
 4 ┛┗┛┗┛┃\○/
 5 ┓┏┓┏┓┃ /
 6 ┛┗┛┗┛┃ノ)
 7 ┓┏┓┏┓┃
 8 ┛┗┛┗┛┃
 9 ┓┏┓┏┓┃
10 ┛┗┛┗┛┃
11 ┓┏┓┏┓┃
12 ┛┗┛┗┛┃
13 ┓┏┓┏┓┃
14 ┃┃┃┃┃┃
15 ┻┻┻┻┻┻
16 */
17 #include <algorithm>
18 #include <iostream>
19 #include <iomanip>
20 #include <cstring>
21 #include <climits>
22 #include <complex>
23 #include <fstream>
24 #include <cassert>
25 #include <cstdio>
26 #include <bitset>
27 #include <vector>
28 #include <deque>
29 #include <queue>
30 #include <stack>
31 #include <ctime>
32 #include <set>
33 #include <map>
34 #include <cmath>
35 using namespace std;
36 #define fr first
37 #define sc second
38 #define cl clear
39 #define BUG puts("here!!!")
40 #define W(a) while(a--)
41 #define pb(a) push_back(a)
42 #define Rint(a) scanf("%d", &a)
43 #define Rll(a) scanf("%lld", &a)
44 #define Rs(a) scanf("%s", a)
45 #define Cin(a) cin >> a
46 #define FRead() freopen("in", "r", stdin)
47 #define FWrite() freopen("out", "w", stdout)
48 #define Rep(i, len) for(int i = 0; i < (len); i++)
49 #define For(i, a, len) for(int i = (a); i < (len); i++)
50 #define Cls(a) memset((a), 0, sizeof(a))
51 #define Clr(a, x) memset((a), (x), sizeof(a))
52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
53 #define lrt rt << 1
54 #define rrt rt << 1 | 1
55 #define pi 3.14159265359
56 #define RT return
57 #define lowbit(x) x & (-x)
58 #define onenum(x) __builtin_popcount(x)
59 typedef long long LL;
60 typedef long double LD;
61 typedef unsigned long long ULL;
62 typedef pair<int, int> pii;
63 typedef pair<string, int> psi;
64 typedef pair<LL, LL> pll;
65 typedef map<string, int> msi;
66 typedef vector<int> vi;
67 typedef vector<LL> vl;
68 typedef vector<vl> vvl;
69 typedef vector<bool> vb;
70
71 const int maxn = 5050;
72 int n;
73 int dp[2][maxn];
74 char s[maxn];
75 char t[maxn];
76
77 int main() {
78     // FRead();
79     while(~Rint(n)) {
80         Cls(t); Cls(dp);
81         Rs(s+1);
82         For(i, 1, n+1) t[n-i+1] = s[i];
83         For(i, 1, n+1) {
84             For(j, 1, n+1) {
85                 if(s[i] == t[j]) dp[i%2][j] = max(dp[i%2][j], dp[(i-1)%2][j-1]+1);
86                 dp[i%2][j] = max(dp[i%2][j], max(dp[(i-1)%2][j], dp[i%2][j-1]));
87             }
88         }
89         printf("%d\n", n - dp[n%2][n]);
90     }
91     RT 0;
92 }
时间: 2024-10-15 10:55:08

[POJ1159]Palindrome(dp,滚动数组)的相关文章

poj 1159 Palindrome lcs+滚动数组

点击打开链接题目链接 Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52910   Accepted: 18248 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

poj - 1159 - Palindrome(滚动数组dp)

题意:一个长为N的字符串( 3 <= N <= 5000),问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->>状态:dp[i][j]表示第i个字符到第j个字符组成的字符串变成回文串的最少插入次数. 状态转移方程: 若sz[i] == sz[j],则:dp[i][j] = dp[i + 1][j - 1]; 否则:dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1; 提交,5

HDU 3392 Pie(DP+滚动数组)

题意:有一些男生女生,男生女生数量差不超过100 ,男生女生两两配对.要求求出一种配对方法,使每一对的高度差的和最小. 思路:(我是真的笨笨笨!!磨磨唧唧写一堆是因为我笨!我看了别人的博客,思路全是学别人的,轻喷!)设人少的一组人数为n,b[],人多的一组人数为m,g[](b[],g[]先排好序),用dp[i][j]表示n中的前i个人与m中的前j个人配对所得到的最小值. 那么dp[i][j]就是min(dp[i-1][k]+|b[i]-g[k]|),就是n中前i-1个人和m中前1~k个人配对的最