POJ 3276 the Cow Lexicon dp-string match

Source: Internet
Author: User

Click to open link
The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8325 Accepted: 3934

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

Usaco February Silver

give you a template string, and then give you the length of this pass, give you n words, let you ask at least how many letters on the template string to delete, so that the template string can be composed of the words given. record the position and length of the last element of each word, looking backward from the beginning of the template string, if the element at a certain position in the template string equals the last character of a word, then from this word start to match, if the match is successful, the judgment is better. state transition Equation dp[i]=min (dp[i],dp[x-1]+differ); Dp[i] Indicates that the minimum number of elements in the template string I need to be removed, x-1 represents the template string forward x position to match the word success, differ represents the number of words that do not match the word during the match.
160k0ms#include<stdio.h> #include <string.h> #include <algorithm>using namespace Std;int dp[1007] ; Char s[307];struct s{char word[27];//word int len;//word length char lastword;//word last element}p[607];int main () {int N,len    ;        while (scanf ("%d%d", &n,&len)!=eof) {scanf ("%s", s+1);            for (int i=0;i<n;i++) {scanf ("%s", P[i].word);            P[i].len=strlen (P[i].word);        P[I].LASTWORD=P[I].WORD[P[I].LEN-1];        } memset (Dp,0,sizeof (DP));                for (int i=1;i<=len;i++) {dp[i]=dp[i-1]+1;//moves +1 for (int j=0;j<n;j++) each backwards                    if (P[j].lastword==s[i]&&i>=p[j].len)//If the template string I position element is equal to the last element of the first J Word and I is longer than the word length {                    int differ=0,x=i,flag=0;                            for (int k=p[j].len-1;x>=1;x--) if (P[j].word[k]==s[x]) {                            k--; if (k<0) {flag=1;break;} K<0 shows that the word is all matched} else differ++;//the number of unequal elements in the match process if (FLA                g) Dp[i]=min (Dp[i],dp[x-1]+differ);    }} printf ("%d\n", Dp[len]); } return 0;}


POJ 3276 the Cow Lexicon dp-string match

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.