The cow lexicon
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:7909 |
|
Accepted:3711 |
Description
Few know that the cows have their own dictionaryW(1 ≤W≤ 600) words, each containing no more 25 of the characters 'A '.. 'Z '. their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. for instance, Bessie once received a message that said "browndcodw ". as it turns out, the intended message was "browncow" and the two letter "D" s were noise from other parts of the barnyard.
The cows want you to help them decipher a received ed message (also containing only characters in the range 'A' .. 'Z') of LengthL(2 ≤L≤ 300) characters that is a bit garbled. in particle, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.
Input
Line 1: two space-separated integers, respectively:
WAnd
L
Line 2:
LCharacters (followed by a newline, of course): The received message
Lines 3 ..
W+ 2: The cows 'dictionary, one word per line
Output
Line 1: A single integer that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.
Sample Input
6 10browndcodwcowmilkwhiteblackbrownfarmer
Sample output
2
Question: How to delete the minimum number of letters so that the strings are the strings listed below.
Dynamic Planning. Here is the DP equation from the past to the future.
The AC code is as follows:
# Include <iostream> # include <cstring> # include <algorithm> using namespace STD; int min (int A, int B) {return a <B? A: B;} int main () {int n, m; int I, j; char a [610], B [1005] [1005]; int DP [610]; int la, Lb; int X, Y; while (CIN> N> m) {CIN> A + 1; for (I = 0; I <N; I ++) CIN> B [I] + 1; DP [0] = 0; for (I = 1; I <= m; I ++) {DP [I] = DP [I-1] + 1; // if not matched for (j = 0; j <n; j ++) {lB = strlen (B [J] + 1); X = 0; y = LB; if (I> = LB & A [I] = B [J] [LB]) {While (I-x> 0) {if (a [I-x] = B [J] [Y]) {x ++; y --;} else x ++; If (! Y) {DP [I] = min (DP [I], DP [I-x] + X-lb); break ;}}}}} cout <DP [m] <Endl;} return 0 ;}