The Cow Lexicon

Problem Description

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.

 
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 that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.
 
Sample Input
6 10 browndcodw cow milk white black brown farmer
 
Sample Output
2
******************************************************************************************************
本题的状态转移方程 dp[i]=min(dp[j]+seg);即i为末尾,j为枚举单词表中的单词数某个单词到目标句子中的j个单词结束所删最小
******************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 int dp[1001];
 9 int i,n,m;
10 char str[605][403];
11 char po[500];
12 int min(int a,int b)
13  {
14      return a>b?b:a;
15  }
16 int  find(int end)//找以i为终点的的最优解
17  {
18      int min2=1005;
19      int len;
20      int j,k;
21      for(int h=1;h<=n;h++)
22       {
23           len=strlen(str[h]);
24            if(str[h][len-1]==po[end]&&end>=len-1)
25            {
26                 for(j=len-1,k=end;j>=0&&k>=0;k--)
27                   if(str[h][j]==po[k])j--;
28                 if(j==-1)
29                 {
30                  if(k==-1)
31                   min2=min(min2,end-k-len);
32                  else
33                   min2=min(min2,dp[k]-k-len+end);
34                 }
35            }
36 
37 
38       }
39     //cout<<min2<<endl;
40      return min2;
41  }
42 
43  int main()
44  {
45      cin>>n>>m;
46      memset(dp,0,sizeof(dp));
47      scanf("%s",po);
48      getchar();
49      for(i=1;i<=n;i++)
50       scanf("%s",str[i]);
51      for(i=0;i<m;i++)
52       {
53           if(i==0)
54             dp[0]=min(1,find(0));
55            else
56             dp[i]=min(dp[i-1]+1,find(i));
57       }
58     cout<<dp[m-1]<<endl;
59     return 0;
60  }
View Code
原文地址:https://www.cnblogs.com/sdau--codeants/p/3247491.html