POJ3267 the Cow Lexicon (dp+ censored)

Source: Internet
Author: User

The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9041 Accepted: 4293

Description

Few know that the cows has their own dictionary with W (1≤ W ≤600) words, each containing no more 25 Of the characters ' a ' ... ' Z '. Their cowmunication system, based on mooing, was not very accurate; Sometimes they hear words. For instance, Bessie once received a message that said "Browndcodw". As it turns out, the intended message is "Browncow" and the "D" s were noise from other parts of the barnyard.

The cows want you-to-help them decipher a received message (also-containing only characters in the range ' a ' ... ' Z ') of length L (2≤ l ≤300) characters that is a bit garbled. In particular, they know that the message had some extra letters, and they want you to determine the smallest number of Le Tters that must is 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 So is the smallest number of characters so need to being removed to make the message a sequence of dictionary words.

Sample Input

6 10browndcodwcowmilkwhiteblackbrownfarmer

Sample Output

2

Source

Dp[i] indicates that the message needs to be removed from the I position by a few letters

Dp[i] = dp[i + 1] + 1//indicates that it does not match at I position, delete the letter I position,

Dp[i] = min (dp[i],dp[i + len + t] + t)//For words with Len length, starting from I need to delete T letters to match exactly, i+len+t means the next one after matching, dp[i+len+t] Is the number of letters that need to be deleted to match the next successful location

#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;Chardic[605][ -];Charmess[ -+Ten];intw,l,dp[605];intDP (intXintLeninty) {    intj =1, tot =0;  while(x <=l) {if(Mess[x] = =Dic[y][j]) J++; Elsetot++; if(j = len +1)//J==len's time is not over, but also to match Len position, Len is the last            returntot; X++; }    return-1;}intMain () { while(SCANF ("%d%d", &w, &l)! =EOF) {scanf ("%s", Mess +1);  for(inti =1; I <= W; i++) {scanf ("%s", Dic[i] +1); } memset (DP,0,sizeof(DP));  for(inti = l; i >0; i--) {Dp[i]= Dp[i +1] +1;  for(intj =1; J <= W; J + +)            {                intLen = strlen (Dic[j] +1); intt =DP (i, Len, j); if(t! =-1) {Dp[i]= Min (Dp[i], dp[i + len + t] +t); }}} printf ("%d\n", dp[1]); }    return 0;}
View Code

POJ3267 the Cow Lexicon (dp+ censored)

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.