Question 1633: [usaco Feb] The cow lexicon dictionary time limit: 5 sec memory limit: 64 MB
Submit: 401 solved: 216
[Submit] [Status] Description
Few people know that cows have their own dictionaries, which contain W (1 ≤ W ≤ 600) Words. Each word cannot exceed 25 characters in length and is composed of lowercase letters. during communication, the words are always inaccurate for various reasons. for example, Bessie heard her say "browndcodw", which means "browncow". There are two more "D" and the two "D" are probably noisy. the cows find it difficult to identify the strange information, so they want you to help identify a message they have received, that is, a string containing only lowercase letters and length of L (2 ≤ L ≤ 300. sometimes, this string contains extra letters, your task is to find out at least a few letters to make this string an accurate "niu Yu" (that is, an arrangement of some words in the cow dictionary ).
Input
Row 1st: two integers separated by spaces, W and L.
Row 2nd: A string with a length of L, indicating the received information. Row 3rd to row W + 2: The Dictionary of the cow, with one word per line.
Output
A unique row: an integer that can be accurate to "niu Yu" by removing at least a few letters ".
Sample input6 10
Browndcodw
Cow
Milk
White
Black
Brown
Farmer
Sample output2
Question
The dp of this question was not thought out at the beginning. I wrote an O (N ^ 3) but I didn't want to write it too much... Go to hehe .... F [I] indicates the number of characters to be deleted before I, F [I] = min {f [I + 1] + 1, f [I + Len [a] + T] + t} t is the number of letters to be deleted after I starts .. Then we can make a variety of optimizations, such as pushing from the back!
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 using namespace std; 6 int n,l,len[605],f[605]; 7 char str[605],ch[605][30]; 8 inline int cal(int x,int len,int y){ 9 int tot=0;10 int l1=x,l2=1;11 while(l1<=l){12 if(str[l1]==ch[y][l2])l2++;13 else tot++;14 if(l2==len+1)return tot;15 l1++;16 }17 return -1;18 }19 int main(){20 scanf("%d%d",&n,&l);21 scanf("%s",str+1);22 for(int i=1;i<=n;i++)23 scanf("%s",ch[i]+1);24 for(int i=l;i;i--){25 f[i]=f[i+1]+1;26 for(int j=1;j<=n;j++){27 int Len=strlen(ch[j]+1);28 int t=cal(i,Len,j);29 if(t!=-1)f[i]=min(f[i],f[i+Len+t]+t);30 }31 }32 printf("%d",f[1]);33 return 0;34 }
View code
Bzoj 1633: [usaco2007 Feb] The cow lexicon ox dictionary