Give some words and a sentence, and ask how many letters are removed from the sentence, which is completely composed of the given words.
DP [I] indicates the minimum number of letters to be removed from the first I digit of a sentence. The status transfer is as follows.
The last letter has two options: delete and retain, if Delete, then DP [I] = DP [I-1] + 1.
If not deleted, enumerate the words ending with str [I] to see if they can be inserted into the first I bit of STR and obtain the optimal value.
Code posted online =
# Include <iostream> # include <algorithm> # include <cmath> using namespace STD; int W, L, DP [305]; char STR [305], dict [601] [305]; int FN (INT pt) {int K, I, j, Mim, Len; MIM = 305; for (I = 0; I <W; I ++) {Len = strlen (dict [I]); If (STR [pt] = dict [I] [len-1] & Len <= Pt + 1) {// word ending with this character for (j = PT, K = len-1; j> = 0 & K> = 0; j --) if (STR [J] = dict [I] [k]) k --; If (k =-1) {If (j =-1) mim = min (MIM, Pt-J-len); // when STR is also searched, the length of else MIM = m is returned. In (MIM, DP [J] + Pt-J-len) ;}}// cout <MIM <Endl; return MIM;} int main () {int I; while (scanf ("% d", & W, & L )! = EOF) {memset (DP, 0, sizeof (DP); scanf ("% s", STR); for (I = 0; I <W; I ++) scanf ("% s", dict [I]); for (I = 0; I <L; I ++) {if (I = 0) DP [0] = min (1, FN (0); // separate, afraid that the word with a length of 1 appears in the dictionary. Else DP [I] = min (DP [I-1] + 1, FN (I);} printf ("% d \ n", DP [L-1]);} return 0 ;}
View code