动态规划——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 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题目大意:一串字符,从左往右读和从右往左读是完全一样的。解题思路:这个题目可以转化为求最长公共子串的问题,就是将原字符串逆转,然后求原字符串和逆转字符串的最长公公子串,最后要注意c数组要定义成short型,否则会超内存

程序代码:

 1 #include<cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 #define MAX 5010
 5 char a[MAX],b[MAX];
 6 short c[MAX][MAX];
 7 int max(int x,int y)
 8 {
 9     return x>y?x:y;
10 }
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     scanf("%s",a);
16     for(int i=n-1;i>=0;i--)
17         b[n-i-1]=a[i];
18         b[n]=‘\0‘;
19     memset(c,0,sizeof(c));
20     for(int i=0;i<n;i++)
21         for(int j=0;j<n;j++)
22     {
23         if(a[i]==b[j])
24             c[i+1][j+1]=c[i][j]+1;
25         else
26             {
27                 if(c[i+1][j]>c[i][j+1])
28                     c[i+1][j+1]=c[i+1][j];
29                 else  c[i+1][j+1]=c[i][j+1];
30             }
31     }
32     printf("%d\n",n-c[n][n]);
33
34 return 0;
35 }

时间: 2024-08-02 14:33:57

动态规划——G 回文串的相关文章

集训第五周动态规划 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 t

集训第五周动态规划 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

动态规划——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

poj 1159 Palindrome -- 回文串,动态规划

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

(Manacher Algorithm, 中心拓展法,动态规划) leetcode 5. 最长回文串

解法一:中心拓展法.从下标为0开始遍历,将每个元素当作回文串中心,向两边拓展. 1)以这个字符为中心的回文串的长度(奇数串): 2)以这个字符和下个字符为中心的回文串的长度(偶数串). 注意:既要统计回文串为奇数时,又要统计回文串为偶数时.当 s[left]!=s[right] 时,left多减了1,right多加了1,所以在计算回文串开头时要把left+1,长度要是(right-1)-(left+1)-1 = right - left -1 class Solution { public: s

[haoi2009]求回文串

所谓回文串,就是对于给定的字符串,正着读和反着读都一样,比如ABCBA就是一个回文串,ABCAB则不是.我们的目标是对于任意输入的字符串,不断将第i个字符和第i+1个字符交换,使得该串最终变为回文串.求最少交换次数. 题解: 有一种做法是贪心: 就是每次找到最左端的字符,然后找到这序列中最右边的一样的字符,然后把这个字符移过去,然后把最左端右移,继续以上操作: 最后的答案就是每次的移动步数加起来: 要吐槽的是,window下I64d不要忘了...... #include<iostream> #

lintcode 容易题:Palindrome Partitioning 分割回文串

题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa","b"], ["a","a","b"] ] 解题: 这个题目不好搞啊,需要动态规划 在这里,没有根据动态规划,也解决了,貌似是暴力解决 从下标pos开始,找到下标i使得 pos到i内是回文字符串,再从i+1开始,找到下一

1112个人赛,最长回文串常见算法讨论

ps.此贴大部分文字与代码来自网上,我只是取长补短整理了下 S=“c a b a”  那么  S' = “a b a c”, 这样的情况下 S和 S‘的最长公共子串是aba.没有错误. 但是当 S=“abacdfgdcaba”, 那么S’ = “abacdgfdcaba”. 这样S和S‘的最长公共子串是abacd.很明显abacd并不是S的最长回文子串,它甚至连回文都不是. 现在是不是都明白为什么最长回文子串不能转化成为最长公共子串问题了.当原串S中含有一个非回文的串的反序串的时候,最长公共子串

LeetCode 5:Longest Palindromic Substring(最长回文串)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串.第一个满足要求的回文串最长. public class S