Poj_3267 the cow Lexicon (DP)

Source: Internet
Author: User
/* It has been several days since I got this question. Because I went home from the National Day holiday, my family just broke the network and never did it. I went back to school today.
The school finally finished it. It indicates that it has been abused by DP for N years, but it is still not necessary. It is only possible to keep the report of the problem solving by DP past day.
Child. T_t...

Question: Give a mes [] and a dictionary DIC [] to find the minimum element to be deleted when the string in the dictionary is completely matched.
Number.

For more information, see http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122638.html:

Match by MES [] string from the back to the back. The worst case is DP [I] = DP [I + 1] + 1;

Set pm to the subscript of MES []. If the section from MES [I] to MES [PM] matches a string in DIC,
DP [I] = min (DP [I], DP [PM] + pm-I-len ). (PM-I indicates the length of this string, and Len indicates DIC []
The length of the matched string. PM-I-Len is the minimum number of characters that are successfully matched by the [I, PM] segment .) */

My code (141 MS ):

# Include <iostream>
# Include <cstring>
# Include <string>
# Include <cstdio>

Using namespace STD;

Const int n= 305;

Int DP [N];
Char mes [N];
String DIC [N * 2];

Int main (){
// Freopen ("data. In", "r", stdin );

Int n, l, I, J, Len, PM, PD;
While (~ Scanf ("% d", & N, & L )){

Memset (mes, 0, sizeof (MES ));
Memset (DP, 0, sizeof (DP ));

For (I = 0; I <n; I ++ ){
Dic [I]. Clear ();
}

Getchar ();
For (I = 0; I <L; I ++ ){
Scanf ("% C", & mes [I]);
}

For (I = 0; I <n; I ++ ){
Cin> DIC [I];
}

For (I = L-1; I> = 0; I --){
DP [I] = DP [I + 1] + 1;
For (j = 0; j <n; j ++ ){
Len = DIC [J]. Length ();
If (LEN <= L & DIC [J] [0] = mes [I]) {// modify it here, change the original Len <L-1 to Len <= L. Because the words in the dictionary may be exactly the same as that in the ox, the result is 0 ));
PM = I;
Pd = 0;
While (PM <L ){
If (DIC [J] [Pd] = mes [PM])
PD ++;
PM ++;
If (Pd = Len ){
DP [I] = min (DP [I], DP [PM] + pm-I-len );
}
}
}
}
}
Printf ("% d \ n", DP [0]);
}
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.