Dp:cheapest palindrome (POJ 3280)

Source: Internet
Author: User

            

Value Minimum palindrome string

To give you a string, you can delete can be added, and every time on a letter of operation with a right, ask you to turn into a palindrome optimal operand.

If I tell you this question, you undoubtedly know that the problem is LD (Levenshtien Distance editing distance), but there is too much nonsense, it is still a bit difficult to understand, for example, I initially felt that palindrome can only be added from the beginning or the end (English eat Xiang ╮(╯▽╰)╭).

Well, actually this question is not the water question (my feeling), this question is very good, is a right editing distance problem, because finally still is old question, asks you the minimum value, therefore immediately thought uses two dimensional matrix, but this question first must solve two traps.

The first trap is the title palindrome, in fact, this problem and palindrome string relationship is not, is in the baseline state (empty string or a letter when used).

The second trap is to delete and join the problem, you look at the topic of the conditions, you will find that both rights are positive, both are positive, this shows that both operations are equivalent (a bit like LD), and finally you find the minimum value, so we only use the smallest value of attention to be able to

But palindrome string we can not all enumerate out, and LD requirements are fixed string, then how to do? In fact, we can, from the smallest start, a small string to find, anyway, delete and join are equivalent operations, we can record the minimum number of operations of these operations, then the last required value on dp[0][length-1]

State transfer equation is almost the same as LD, but less input[j]!=input[i] when compared with dp[i+1][j-1] (this is equivalent to the modification, modify that is Delete + add, certainly unreasonable)

DP[I][J]=DP[I+1][J-1]; INPUT[I]==INPUT[J];

Dp[i][j]=min{min (Dp[i+1][j]+add[input[i]],dp[i][j]), Min (Dp[i][j-1]+add[input[j]]+add[input[j]])};

  

Reference: http://blog.csdn.net/y990041769/article/details/24259569

1#include <stdio.h>2#include <stdlib.h>3 #defineMax_n 20014 #defineMIN (a) < (b) ( A):(B))5 6 Static intword_dist[ -];7 Static CharInput[max_n];8 Static intDp[max_n][max_n];9 Ten voidSearch (Const int,Const int); One  A intMainvoid) - { -     intstring_length, C_word_sum, I, Tmp_add, Tmp_de; the     Chartmp; -  -      while(~SCANF ("%d%d", &c_word_sum, &string_length)) -     { +GetChar ();//Get rid of the carriage -scanf"%s", input); +          for(i =0; i < c_word_sum; i++) A         { atGetChar ();//Get rid of the carriage -scanf"%c", &tmp); -scanf"%d%d", &tmp_add, &tmp_de); -Word_dist[tmp-'a'] =MIN (Tmp_add, tmp_de); -         } - Search (String_length, c_word_sum); in     } -     return 0; to } +  - voidSearch (Const intString_length,Const intc_word_sum) the { *     //the last minimum value is inherited to Dp[0][string_length-1] $     intI, J, K;Panax Notoginseng      for(i =1; i < string_length; i++) -     { the          for(j =0, k = i; K < String_length; J + +, k++) +         { ADP[J][K] =Int_max; the             if(Input[j] = =Input[k]) +Dp[j][k] = dp[j +1][k-1]; -             Else $             { $Dp[j][k] = MIN (dp[j +1][K] + word_dist[input[j]-'a'], dp[j][k]); -Dp[j][k] = MIN (Dp[j][k-1] + word_dist[input[k]-'a'], dp[j][k]); -             } the         } -     }Wuyiprintf"%d\n", dp[0][string_length-1]); the}

Dp:cheapest palindrome (POJ 3280)

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.