Palindrome Series Interval DP

Source: Internet
Author: User

UVa 10739 string to Palindrome (classic Palindrome range DP)

Test instructions

Given a string, you can delete, insert, and replace the operation.

Ask at least a few times, you can make this string a palindrome string.

Ideas:

look at others, the optimization problem, with a more direct approach to find the situation is very complex, many times it is necessary to consider dynamic planning. First, from the whole, from large to small, looking at the situation of a few two elements of the optimal recursion, How to get the results.

(Here the interval DP is constantly expanding to both sides of the scale)

(1) If the outermost two characters are the same, s[0]==s[n], then the outer side of these two characters must not be considered, as long as the internal optimal.

DP[0][N]=DP[0+1][N-1];

(2) If the outermost two characters are not the same, s[0]!=s[n], this must be done. Consider its sub-palindrome string has Zuo Huiwen [0,n-1], right palindrome [1,n], and the middle palindrome [1,n-1] Three, when the sub-palindrome optimal, only one operation can be done. For double palindrome This does not need to consider, because it must be transferred to the three sub-palindrome in one, the cost of >= palindrome, not considered. As long as the transfer cost is optimal, the sub-palindrome optimal, the results are optimal (DP basic ideas, not yet cooked).

Adding and removing operations on the left and right palindrome feasible, then Dp[0][n]=min (dp[1][n]+1,dp[0][n-1]+1), and the operation to three kinds of conditions are feasible dp[0][n]=min (Dp[1][n]+1,dp[0][n-1]+1,dp[1] [n-1]+1]; change 0,n to I,j;

Fully dp[i][j]=min (min (dp[i + 1][j], dp[i][j-1]) + 1,dp[i + 1][j-1] + 1);

#define_crt_secure_no_deprecate#include<cstdio>#include<cmath>#include<cstring>#include<ctype.h>#include<iostream>#include<algorithm>#include<vector>using namespacestd; #include<cstdio>#include<cstdlib>#include<cstring>Const intMAXN =1010;CharSTR[MAXN];intDP[MAXN][MAXN];intMain () {intcases, CC =0; scanf ("%d", &cases);  while(cases--) {scanf ("%s", str); intn =strlen (str);  for(inti =0; I < n; ++i) {Dp[i][i]=0; if(Str[i] = = Str[i +1]) Dp[i][i+1] =0; ElseDp[i][i+1] =1; }         for(intp =2; P < n; ++p) { for(inti =0, j = p; J < N; ++i, + +j) {if(Str[i] = =Str[j]) dp[i][j]= Dp[i +1][j-1]; ElseDp[i][j]=min (min (dp[i +1][J], Dp[i][j-1]) +1, Dp[i +1][j-1] +1); }} printf ("Case %d:%d\n", ++CC, dp[0][n-1]); }    return 0;}

Source: http://www.cnblogs.com/kedebug/archive/2012/11/19/2777249.html

Palindrome Series Interval DP

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.