POJ1159:动态规划

Palindrome

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 60290   Accepted: 20998

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
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=5005;
unsigned short int dp[MAXN][MAXN];//dp[i][j]表示以第i个字母开头,第j个字母结尾的构成回文子串所需添加的字符串
char s[MAXN];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s);
        for(int i=0;i<n;i++)
        {
            dp[i][i]=0;//有一个字符时
        }
        for(int i=1;i<n;i++)
        {
            if(s[i-1]==s[i])
                dp[i-1][i]=0;
            else
                dp[i-1][i]=1;
        }
        for(int k=2;k<n;k++)
        {
            for(int i=0,j=k;j<n;i++,j++)
            {
                if(s[i]==s[j])
                    dp[i][j]=dp[i+1][j-1];
                else
                    dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;
            }
        }
        printf("%d\n",dp[0][n-1]);
    }
    return 0;
}
时间: 2024-08-04 02:49:39

POJ1159:动态规划的相关文章

poj1159(动态规划或者lcs求最长字串)

http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=20480 LCS求最长公共子串 #include<stdio.h> #include<iostream> #include<map> #include<string.h> #include<vector> using namespace std; short a[5005][5005]; char s1[5005]; c

POJ1159 Palindrome 【动态规划】

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52571   Accepted: 18124 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

Palindrome(poj1159)(动态规划)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53877   Accepted: 18610 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

算法初步:再讨论一点动态规划

原创 by zoe.zhang 动态规划真的很好用啊,但是需要练习,还有很多技巧要学习. 1.滚动数组 动态规划是用空间换取时间,所以通常需要为DP数组开辟很大的内存空间来存放数据,但有的时候空间太大超过内存限制,特别是在OJ的时候,容易出现MLE的问题.而在一些动规的题目中,我们可以利用滚动数组来优化空间. 适用条件:DP状态转移方程中,仅仅需要前面若干个状态,而每一次转移后,都有若干个状态不会再被用到,也就是废弃掉,此时可以使用滚动数组来减少空间消耗. 优点:节约空间. 缺点:在时间消耗上没

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种

活动选择的贪心算法与动态规划(未完成)

// greedy_algorithm.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<queue> using namespace std; #define NofActivity 11 int c[NofActivity + 1][NofActivity + 1]; int reme[NofActivity + 1][NofActivity + 1]; //活动的

求不相邻金币相加和的最大值--动态规划1

求不相邻金币相加和的最大值. 输入n个金币的金币面值(正数自定义),求这些金币不相邻和的最大值. 动态规划问题1 设f(n)为第n个金币数的最大值,f(0)=0,f(1)=a[1],输入的数组从下标为1开始. f(n)=max{a[n]+f(n-2),f(n-1)}. 代码如下: import java.util.Scanner; public class Jin_bi_zui_da_zhi { public static void main(String[] args) { Scanner s

[动态规划] 黑客的攻击 Hacker&#39;s CrackDown Uva 11825

抽象为数学模型就是,  取尽可能多的互不相交的子集 ,  使得每一个子集都能覆盖全集 #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int n; int P[1000],cover[1000],f[1000]; int main(){ scanf("%d", &n); for (int i = 0; i < n;i++) {

Beauty Of algorithms(七)动态规划 钢条分割 矩阵链乘 最长公共子序列 最优二叉树

1.动态规划                动态规划的方法与方法类似,英文"dynamic programming",这里的programming不是程序的意思,而是一种表格法.都是通过组合子问题的解来解决原问题,分治方法将划分为互不相交的子问题,递归的求解子问题,再将它们的解组合起来求出原问题的解.与之相反动态规划应用于子问题的重叠情况,即不同的子问题具有公共的子问题,子问题的求解是递归进行 的,将其划分为更小的子问题,动态规划,每个子问题只求解一次,将其保存在表格中,从而无需每次求