1633: [Usaco2007 feb]the Cow Lexicon cow's DictionaryTime Limit:5 Sec Memory limit:64 MB submit:582 solved:321 [submit][status][discuss]
Description
Few people know that cows have their own dictionaries with W (1≤w≤600) words, each with a length of not more than 25, and consisting of lowercase letters. When they communicate, the words are always inaccurate for a variety of reasons. For example, Bessie heard someone say "Browndcodw" to her, The exact meaning is "browncow", more than two "D", these two "D" is probably the noise around. The cows find it hard to identify the strange information, so they want you to help identify a received message, a string containing only a lowercase letter and a length of L (2≤l≤300). Sometimes there are extra letters in this string, Your task is to find a minimum number of letters to make the string into an accurate "cow" (that is, an arrangement of some words in a dairy dictionary). Input
Line 1th: Two integers separated by a space, W and L.
Line 2nd: A string of length l that represents the information received. Line 3rd to w+2: Cow's dictionary, one word per line. Output
Unique line: An integer that means that at least a few letters are removed to make it an accurate "cow language".
Sample Input 6 10
Browndcodw
Cow
Milk
White
Black
Brown
Farmer
Sample Output 2
HINT
Source
Silver
[Submit] [Status] [Discuss]
F[i] Indicates the minimum number of characters to be deleted before overwriting the first I characters
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int n=1005;
int n,l,f[n];
Char a[n],c[n][35];
int main ()
{
scanf ("%d%d", &n,&l);
scanf ("%s", a+1);
for (int i=1;i<=n;i++) scanf ("%s", c[i]+1);
for (int i=1;i<=l;i++)
{
f[i]=i;
for (int j=1;j<=n;j++)
{
int len=strlen (c[j]+1), cnt=0,k;
for (k=i;k;k--)
{
if (c[j][len]==a[k]) len--;
else cnt++;
if (len==0) break;
}
if (len==0) f[i]=min (f[i],f[k-1]+cnt);
}
}
printf ("%d", f[l]);
}