POJ 1159 Palindrome (interval dp/longest common sub-series + scrolling Array)

Source: Internet
Author: User
Tags cmath

Palindrome
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 56150 Accepted: 19398

Description

A palindrome is a symmetrical string, which is, a string read identically from the left to the right as well as from the right to left. You-to-write a program which, given a string, determines the minimal number of characters to being inserted into the STR ing in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "ab3bd" can is transformed into a palindrome ("Dab3bad" or "Adb3bda") . However, inserting fewer than 2 characters does not produce a palindrome.

Input

Your program was 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 is to be considered distinct.

Output

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

Sample Input

5ab3bd

Sample Output

2

Give a string that calculates a minimum number of characters to make the string a palindrome (that is, from the post-read to the backward-forward reading).

There are 2 kinds of ideas, one is the direct interval dp,dp[j][i] means [i,j] this substring to become a palindrome need to add how many characters, the state transition equation is as follows:
if (S[i]==s[j])
DP[J][I]=DP[J+1][I-1];
Else
Dp[j][i]=1+min (Min[j+1][i],min[j][i-1])
The second way of thinking is also relatively easy to think, to change a string into a palindrome string, then we can get the string in reverse order, and then find the two longest common sub-sequence, the number of characters to be added is the length of the string minus the longest common subsequence length.

In addition, this problem will limit memory. If you define an array of 5000*5000, the memory Limit exceeded. There are two solutions, one is to define the array as a short type, so that the original memory will be reduced by a large part, probably around 50000kb, just can ac; the other solution: because the first line of the i-1 only need to know the first row, so you can open a 2*5000 rolling DP array, This method is more recommended and saves memory.

/*lcs+shortmemory:49688 kbtime:1094 mslanguage:g++result:accepted*/#include <stack> #include <queue># include<cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm > #pragma commment (linker, "/stack:102400000 102400000") #define Lson a,b,l,mid,cur<<1#define Rson a,b,mid+1,r , cur<<1|1using namespace std;const double eps=1e-6;const int Maxn=5001;char s[maxn],t[maxn];int N,ans,tlen;short int Dp[maxn][maxn];int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%        D ", &n)!=eof) {scanf ("%s ", s+1);        for (int i=n;i>=1;i--) t[n-i+1]=s[i];        t[n+1]=0;        Memset (Dp,0,sizeof (DP)); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (S[i]==t[j]) dp[i][j]=dp[                i-1][j-1]+1;        else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]);    printf ("%d\n", N-dp[n][n]); } return 0;} 
/*dp+shortmemory:55716 kbtime:1454 mslanguage:g++result:accepted*/#include <stack > #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include < iostream> #include <algorithm> #pragma commment (linker, "/stack:102400000 102400000") #define Lson A,b,l,mid, Cur<<1#define Rson a,b,mid+1,r,cur<<1|1using namespace std;const double Eps=1e-6;const int MAXN=5300;char s    [Maxn];short int N,dp[maxn][maxn];int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin); #endif//Online_judge        while (scanf ("%d", &n)!=eof) {scanf ("%s", s);        Memset (Dp,0,sizeof (DP));                    for (int i=1;i<n;i++) for (int j=i-1;j>=0;j--) {if (S[i]==s[j])                DP[J][I]=DP[J+1][I-1];            else dp[j][i]= (short int) (min (dp[j+1][i],dp[j][i-1]) +1);    } printf ("%d\n", dp[0][n-1]); } return 0;} 
/*lcs+ scrolling array memory:728 kbtime:735 mslanguage:g++result:accepted*/#include <stack> #include <queue># include<cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm > #pragma commment (linker, "/stack:102400000 102400000") #define Lson a,b,l,mid,cur<<1#define Rson a,b,mid+1,r , cur<<1|1using namespace std;const double eps=1e-6;const int Maxn=5001;char S[maxn],t[maxn];int n,ans,tlen,dp[2] [Maxn];int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d", &n)! =        EOF) {scanf ("%s", s+1);        for (int i=n; i>=1; i--) t[n-i+1]=s[i];        t[n+1]=0;        Memset (Dp,0,sizeof (DP));        int indexs=0;            for (int i=1; i<=n; i++) {indexs=!indexs;                for (int j=1; j<=n; J + +) if (s[i]==t[j]) dp[indexs][j]=dp[!indexs][j-1]+1; else Dp[indexs][j]=max (Dp[!indexs][J],dp[indexs][j-1]);    } printf ("%d\n", N-dp[indexs][n]); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1159 Palindrome (interval dp/longest common sub-series + scrolling Array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.