Poj 3267 the cow lexicon

Source: Internet
Author: User

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




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.