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

Source: Internet
Author: User

The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8211 Accepted: 3864

Description

Few know that the cows has their own dictionary with   W   (1≤   W   ≤600) words, each containing no more than 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.< /p>

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:  W and  L 
Line 2:  L Characters (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 gives a string that asks at least a few characters to get the sequence that is completely composed of the given dictionary. The dictionary gives the basis questions in the following. The last string that can be thought of is a dictionary, so the words that make up the string are scattered in the given strings at the beginning, and what we are looking for is to infer the position of the word in the string. Dp[i] Indicates the minimum number of characters to be eliminated from beginning to the first character, dp[0] = 1, dp[i] = dp[i-1], matches less than dp[i] = dp[j] + k occurrences of the string can match to the string from j+1 to I, K = from j+1 to I complete match needs to be eliminated Character. It was written with the longest common subsequence, and later found that there were no words to match when looking for Dp[i] from 0 to I. requirements, the last word of the word must be the same as s[i]. Otherwise the word would have been matched before.

So just use the normal traversal to be able. PS: For strings that do not know where to start or end, use the longest common subsequence, assuming that you know a definite position and match directly.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;struct node{int l; Char s[30];} P[700];int Dp[400];char s[400]; int cmp (node A,node b) {return A.L < B.L;}    int main () {int I, j, N, L;        while (scanf ("%d%d", &n, &l)!=eof) {scanf ("%s", s);            for (i = 0; i < n; i++) {scanf ("%s", P[I].S);        P[I].L = strlen (P[I].S);        } sort (p,p+n,cmp);        Memset (Dp,0,sizeof (DP));        Dp[0] = 1;            for (i = 0; i < L; i++) {if (i) dp[i] = dp[i-1]+1;                for (j = 0; P[j].l <= i+1; j + +) {int K1 = i, k2 = p[j].l-1;                if (S[K1]! = P[j].s[k2]) continue;                    while (K1 >= 0 && K2 >= 0) {if (s[k1] = = P[j].s[k2])                        {k1--;                    k2--;      }              else k1--;                } if (K2 = =-1) {Dp[i] = min (DP[I],DP[K1]+I-K1-P[J].L);    }}} printf ("%d\n", Dp[l-1]); } return 0;}

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.