Poj 3267 Dynamic Planning

Source: Internet
Author: User

Question address:

Http://poj.org/problem? Id = 3267

I am not very familiar with how to do dynamic planning for this topic. I saw a post from a classmate. I had an AC question, but I still seem to understand it. I will repost my ideas and think about it.

Question

Dynamic Planning

 

The question is to give a primary string and a dictionary, and ask the minimum number of letters deleted from the primary string to match the word sequence in the dictionary.

PS: match the word sequence, not a word

 

Not to mention, look at the program

It mainly knows the meaning of the state equation.

DP [I] indicates the number of characters deleted from the I-th character in the message to the L-th character (end). The value is initialized to DP [l] = 0.

Since my program is retrieving and matching from the end of the message to the header, it is the following state equation:


 

From the program, we can see that the number of characters deleted from position I to position l always gets the worst case first. The second equation is used for state optimization and updating only when words can be matched.

 

The first equation is not hard to understand. It can be simply deduced if you understand the meaning of DP [I ].

The second difficulty of the equation lies in DP [PM] + (PM-I)-Len

From the program, PM is the pointer of the message (where I indicates the starting position of the current matched word in the message), Pd is the pointer of the dictionary.

The matching process is as follows:

When it is confirmed that the first bit of the message matches the first part of a word, it starts to match word by word. If the characters are the same, the two pointers move backward at the same time. Otherwise, the PD is fixed and the PM is moved. When a match exists due to PM> L, the match fails, and the DP [I] status remains unchanged. When Pd = word length, the match is successful, and the DP [I] status is optimized.

 

Apparently, when the match is successful, PM-I indicates the length of the interval from position I to PM during the match process, and then minus the word length Len, then, the number of characters (PM-I)-len deleted from I to PM is obtained. DP [PM] indicates the number of characters deleted from PM to L (based on the retrieval direction, the value of DP [PM] has been processed as the worst, so it is not a null value)

Therefore, DP [PM] + (PM-I)-len indicates the number of characters deleted from I to L. It is not difficult to prove that this value must be equal or better than DP [I, therefore, assign the min value to DP [I].

This is the most difficult part of this question.

Finally, output DP [0]. I believe everyone understands the meaning of DP [0 ].

 

Source codeproblem: 3267 User: hopeztmmemory: 192 ktime: 79 mslanguage: C ++ result: acceptedsource Code # include <stdio. h> # include <string. h> int min (int A, int B) {return a <B? A: B ;}# define maxword 700 # define maxlen 30 char directionary [maxword] [maxlen]; int nword; int nlen; int strlen [maxword]; // to save time, saves the length. In one loop, the char Query [400]; int DP [400]; int getmindelete () {int I, j; memset (DP, 0, sizeof (DP); for (I = nlen-1; I> = 0; I --) {DP [I] = DP [I + 1] + 1; for (j = 0; j <nword; j ++) {If (nlen-I> = strlen [J] & directionary [J] [0] = Query [I]) // The length of the suffix must be gr Eater than Direction [I]; {int PM = I; int Pd = 0; while (PM <nlen) {If (Query [PM ++] = directionary [J] [Pd]) {PD ++;} If (Pd = strlen [J]) {DP [I] = min (DP [I], DP [PM] + pm-I-strlen [J]); break ;}}}}} return DP [0];} int main () {While (scanf ("% d", & nword, & nlen )! = EOF) {scanf ("% s", query); int I; for (I = 0; I <nword; I ++) {scanf ("% s ", direary ary + I); strlen [I] = strlen (direary ary [I]);} printf ("% d \ n", getmindelete ();} return 0 ;}

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.