POJ-1159 palindrome (Palindrome variant)

Source: Internet
Author: User

D. To add a minimum number of characters to a string to become a palindrome.

S.

The simple approach is to string the longest common subsequence length Len directly against it and its reverse sequence. N-len is the request. (n is the original string length)

This is done for the following reasons:

Requires a minimum number of characters to be added, we can first find a long palindrome from the original string, and then for the original string does not belong to the palindrome string character, in its center of the palindrome string in the symmetric position of the same character can be added. Then the number of characters that need to be added is the N-longest palindrome string length.

The longest palindrome can be seen as a match between the front and back characters in the original string (each subsequent character finds a character that matches the position requirement in front of it). This kind of palindrome matching and the original string and the sequence of the common subsequence is one by one corresponding (a palindrome match a common sub-sequence, and vice versa), and the two involved in the original string of the number of characters is equal, that is, the longest common sub-series corresponding to the longest palindrome string. The reason statement is complete.

There is another way of dynamic planning.

F[I][J] Indicates the minimum number of characters added to the string from I to J if it becomes a palindrome.

if (st[i] = = St[j])
F[I][J] = f[i + 1][j-1];
Else
F[i][j] = min (f[i + 1][j], f[i][j-1]) + 1;

C.

/*use short type insurance. */#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd; Shortdp[5002][5002];intMain () {Chara[5001],s[5001]; intI,j,n; scanf ("%d",&N); scanf ("%s", a); Memset (DP,0,sizeof(DP));  for(i=0, j=n-1; i<n; i++,j--) {S[i]=A[j]; }     for(i=1; i<=n; i++)    {         for(j=1; j<=n; J + +)        {            if(a[i-1]==s[j-1]) Dp[i][j]=dp[i-1][j-1]+1; ElseDp[i][j]=max (dp[i-1][j],dp[i][j-1]); }} printf ("%d\n", N-Dp[n][n]); return 0;}
View Code

PS: In fact, you can use a scrolling array, you do not need short insurance too.

C2.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespacestd;#defineMAXN 5005CharST[MAXN];intN; ShortF[MAXN][MAXN];intMain () {//freopen ("T.txt", "R", stdin);scanf"%d", &N); scanf ("%s", ST);  for(inti = n-1; I >=0; i--) {F[i][i]=0;  for(intj = i +1; J < N; J + +)            if(St[i] = =St[j]) f[i][j]= F[i +1][j-1]; ElseF[i][j]= min (f[i +1][J], F[i][j-1]) +1; } printf ("%d\n", f[0][n-1]); return 0;}
View Code

POJ-1159 palindrome (Palindrome variant)

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.