/* 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;
}