The Cow Lexicon
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 16 Accepted Submission(s) : 10
Few know that the cows have 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, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "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 has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int W,L,dp[301],i,j;
char word[301],dic[601][301];
cin>>W>>L>>word;
for(i=0;i<W;i++)
cin>>dic[i];
dp[L]=0;
for(i=L-1;i>=0;i--)
{
dp[i]=dp[i+1]+1;
for(j=0;j<W;j++)
{
int len=strlen(dic[j]);
if(L-i>=len&&word[i]==dic[j][0])
{
int i1=i,i2=0;
while(i1<L)
{
if(dic[j][i2]==word[i1++])
i2++;
if(i2==len)
{
dp[i]=min(dp[i],dp[i1]+i1-i-len);
break;
}
}
}
}
}
cout<<dp[0]<<endl;
}
。