集训第五周动态规划 G题 回文串

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

这道题类似于编辑距离,使用动态规划可解:

用dp(i,j)表示数组从i到j使其成为回文串需要加入的字符个数

那么动态规划方程为            1.min(dp(i+1,j)+1,dp(i,j-1)+1) (a[i]!=a[j])dp(i,j)={             2.dp(i+1,j-1) (a[i]=a[j])

这道题由于n最大可达5000,因此使用int型的二维数组会超空间,可以把int型改成short型,也可以使用滚动数组。。。。
#include"iostream"
#include"cstring"
#include"cstdio"
using namespace std;

const int maxn=5010;

char a[maxn];
short dp[2][maxn];

short min(short a1,short a2)
{
    return a1<a2?a1:a2;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",a+1);
        for(int i=n-1;i>=1;i--)
        {
            for(int j=i+1;j<=n;j++)
            {
                dp[i%2][j]=min(dp[i%2][j-1]+1,dp[(i+1)%2][j]+1);
                if(a[j]==a[i])
                dp[i%2][j]=dp[(i+1)%2][j-1];
            }
        }
        cout<<dp[1][n]<<endl;
    }
    return 0;
}

				
时间: 2025-01-08 10:46:24

集训第五周动态规划 G题 回文串的相关文章

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

集训第五周动态规划 I题 记忆化搜索

Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.在上面的例子

集训第五周动态规划 F题 最大子矩阵和

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this probl

集训第五周动态规划 J题 括号匹配

Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regul

集训第五周动态规划 K题 背包

Description Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of a

动态规划——H 最少回文串

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a list of one or more disjoint non-empty groups

[编程题-蘑菇街]回文串

[编程题] 回文串 给定一个字符串,问是否能通过添加一个字母将其变为回文串. 输入描述: 一行一个由小写字母构成的字符串,字符串长度小于等于10. 输出描述: 输出答案(YES\NO). 输入例子: coco 输出例子: YES #include<iostream> #include<string> using namespace std; bool f(string& s, int i) { int l = 0, r = s.size() - 1; if (l == i)

动态规划——G 回文串

G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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

NOI题库 / 2.6基本算法之动态规划 - 8471:切割回文

总时间限制:  1000ms 内存限制:  65536kB 描述 阿福最近对回文串产生了非常浓厚的兴趣. 如果一个字符串从左往右看和从右往左看完全相同的话,那么就认为这个串是一个回文串.例如,"abcaacba"是一个回文串,"abcaaba"则不是一个回文串. 阿福现在强迫症发作,看到什么字符串都想要把它变成回文的.阿福可以通过切割字符串,使得切割完之后得到的子串都是回文的. 现在阿福想知道他最少切割多少次就可以达到目的.例如,对于字符串"abaacca