Poj3267 -- the cow Lexicon (DP: string combination)

Source: Internet
Author: User
The cow lexicon
Time limit:2000 ms   Memory limit:65536 K
Total submissions:8211   Accepted:3864

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

Source

Usaco 2007 February silver provides a string. The sequence obtained by removing at least a few characters is completely composed of the given dictionary, we can think of the final string as a dictionary, so the words that make up the string are scattered in the given string at the beginning. What we are looking for is to determine the position of the word in the string. DP [I] indicates the minimum number of characters to be removed from the beginning to the I, DP [0] = 1; DP [I] = DP [I-1], cannot match DP [I] = DP [J] + K. A string can be matched to a string from J + 1 to I, k = characters to be eliminated from J + 1 to I. When I started writing with the longest common subsequence, I later found that when I was looking for the DP [I] to see if there were any words that could match from 0 to I, the last word of a word must be the same as that of S [I]. Otherwise, the word has already been matched. So we only need to use a normal traversal. PS: For a string that does not know the start or end position, use the longest common subsequence. If you know a definite position, match it directly.

Poj3267 -- the cow Lexicon (DP: string combination)

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.