The Cow Lexicon(dp)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7290   Accepted: 3409

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

题意:给出一个由m个字符组成的单词和n个单词表,问至少删除多少个字母使这个单词才能由下面的单词表中的单词组成;
browndcodw 中删除两个d后由brown 和 cow 组成;

不得不说dp题真心难,想了半天没思路,发现自己还是太弱了,BU童鞋给讲的思路;

思路:从最后一个字母开始匹配,先初始化,dp[m] = 0,假使当前字母不能匹配,则dp[i] = dp[i+1] + 1;然后,遍历单词表中每个单词,如果有单词的首字母与当前字母相同,那么该单词有可能和这个字母以后的单词匹配,经判断后若能匹配,就算出匹配成功要删除的字母个数,我的是(cur-i)-len[j],取dp[i] 和 dp[cur]+(cur-i)-len[j] 的较小者


 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<algorithm>

 4 using namespace std;

 5 

 6 char s[310];//待匹配单词

 7 char dict[610][30];//单词表

 8 int dp[310];// dp[i]表示到第i个字母为止要删除的最少字母数;

 9 int len[610];//保存单词表中每个单词的长度;

10 int n,m;

11 

12 int main()

13 {

14     int i,j;

15     scanf("%d %d",&n,&m);

16     scanf("%s",s);

17     for(i = 0; i < n; i++)

18     {

19         scanf("%s",dict[i]);

20         len[i] = strlen(dict[i]);

21     }

22 

23     dp[m] = 0;//初始化

24 

25     for(i = m-1; i >= 0; i--)

26     {

27         dp[i] = dp[i+1] + 1;//初始化

28 

29         for(j = 0; j < n; j++)//遍历每个单词

30         {

31             if(dict[j][0] == s[i] && (m-i) >= len[j])//若有个单词的首字母与s[i]相同

32             {

33                 int cur = i+1, cnt = 1;

34 

35                 while(cur < m && dict[j][cnt])

36                 {

37                     if(s[cur++] == dict[j][cnt])

38                         cnt++;

39                 }//检查是否可以匹配

40 

41                 if(cnt == len[j])//若能匹配,取较小者

42                     dp[i] = min(dp[i],dp[cur]+(cur-i)-len[j]);//(cur-i)-len[j]表示删除的字母个数

43             }

44         }

45     }

46     printf("%d\n",dp[0]);

47     return 0;

48 }
View Code

 





你可能感兴趣的:(icon)