Palindrome_滚动数组&&DP

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 to write a program which, given a string, determines the minimal number of characters to 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

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase 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

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

【题意】给出一个字符串,求插入多少字符才能形成回文串;

【思路】用a数组存储原串,b数组储存倒串,求最长公共子序列,答案用n-最长公共子序列;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=5050;
char a[N],b[N];
//int dp[N][N];//MLE
int dp[2][N];//用滚动数组
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        getchar();
        scanf("%s",a+1);
        for(int i=1;i<=n;i++)
        {
            b[n-i+1]=a[i];
        }
        memset(dp,0,sizeof(dp));
        int mx=0;
        /*for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i]!=b[j])
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
                else dp[i][j]=dp[i-1][j-1]+1;
            }
        }*/
        int e=0;//用滚动数组,节约存储空间!!!!
        for(int i=1;i<=n;i++)
        {
            e=1-e;
            for(int j=1;j<=n;j++)
            {
                if(a[i]==b[j])
                {
                    dp[e][j]=dp[1-e][j-1]+1;
                }
                else
                {
                    if(dp[1-e][j]>dp[e][j-1])
                    {
                        dp[e][j]=dp[1-e][j];
                    }
                    else dp[e][j]=dp[e][j-1];
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            mx=max(mx,max(dp[1-e][i],dp[e][i]));
        }
        int ans=n-mx;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-23 17:40:25

Palindrome_滚动数组&&DP的相关文章

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

ural 2018. The Debut Album 滚动数组dp

点击打开链接 2018. The Debut Album Time limit: 2.0 second Memory limit: 64 MB Pop-group "Pink elephant" entered on recording their debut album. In fact they have only two songs: "My love" and "I miss you", but each of them has a la

URAL 1287. Mars Canals 滚动数组+DP

求从上到下 从左到右 从左上到右下 从右上到左下的 最长的S和s串 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1444; char a[maxn][maxn]; int dp[2][maxn][2][4]; int main() { int n; int ans1 = 0, ans2 = 0; scanf("%d

HDU 4576 简单概率 + 滚动数组DP(大坑)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include <cstdlib> 7 #include <string>

【滚动数组】【状压dp】Gym - 100956F - Colored Path

f(i,j,S)表示到(i,j),且经由的路径上的颜色集合为S的价值的最小值,从上方和左方转移过来即可. 要注意,内存不足,需要滚动数组优化,即使用了map,还是需要. 路径输出的时候,可以再跑一遍dp,这样就不用再开一个大数组了. 我的写法比较菜.卡了常数 #include<cstdio> #include<algorithm> #include<map> using namespace std; int w[401][401],c[401][401]; typede

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]直接接在前面

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个人配对的最

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

vijos 1907 DP+滚动数组

描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告失败. 为了简化问题,我们对游戏规则进行了简化和改编: 游戏界面是一个长为 n,高为 m 的二维平面,其中有k 个管道(忽略管道的宽度). 小鸟始终在游戏界面内移动.小鸟从游戏界面最左边 任意整数高度位置出发,到达游戏界面最右边时,游戏完成. 小鸟每个单位时间沿横坐标方向右移的距离为 1,竖直移动的