码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3267——The Cow Lexicon(动态规划)

时间:2014-11-13 16:05:50      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   sp   for   

The Cow Lexicon


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

题目大意:

    题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列.
    PS:是匹配单词序列,而不是一个单词

解题思路:

    动态规划。网上多数是从后往前推。我从前往后推也AC了。发现更好理解一些。

    具体细节看代码。

Code:

 1 /*************************************************************************
 2     > File Name: poj3267.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com
 5     > Created Time: 2014年10月17日 星期五 18时28分18秒
 6  ************************************************************************/
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstdlib>
10 #include<string>
11 #include<cstring>
12 #include<list>
13 #include<queue>
14 #include<stack>
15 #include<map>
16 #include<set>
17 #include<algorithm>
18 #define MAXN 1001
19 using namespace std;
20 char dic[MAXN][MAXN];
21 char mes[MAXN];
22 int dp[MAXN];
23 int N,M;
24 int Incl(int i,int j) //判断字典j是否包含在字符串前i位内
25 {
26     int Ndic=strlen(dic[j])-1;
27     if (dic[j][Ndic]!=mes[i]) return -1;
28     while (1)
29     {
30         if (Ndic==-1||i==-1) break;
31         if (dic[j][Ndic]==mes[i]) i--,Ndic--;
32         else i--;
33     }
34     if (Ndic==-1) return i+1; //返回字典i匹配的第一个字符的位置。
35     else return -1;
36 }
37 int solve()
38 {
39     for (int i=0; i<=M-1; i++)
40     {
41         if (i!=0)
42             dp[i]=dp[i-1]+1;
43         else
44             dp[i]=1;
45         for (int j=1; j<=N; j++)
46         {
47             int k=Incl(i,j); //判断字典j是否包含在字符串前i位内
48             if (k!=-1)
49             {
50                 //回溯到字符串开头处时,需要特判。注意if语句!WA了好几遍
51                 if (k-1<0&&dp[i]>i+1-k-strlen(dic[j]))
52                     dp[i]=(i+1-k-strlen(dic[j]));
53                 else if (dp[i]>dp[k-1]+i+1-k-strlen(dic[j]))
54                     dp[i]=dp[k-1]+i+1-k-strlen(dic[j]);
55             }
56         }
57     }
58     return dp[M-1];
59 }
60 int main()
61 {
62     while (scanf("%d%d",&N,&M)!=EOF)
63     {
64         memset(dp,0,sizeof(dp));
65         memset(dic,0,sizeof(dic));
66         memset(mes,0,sizeof(mes));
67         scanf("%s",mes);
68         for (int i=1; i<=N; i++)
69             scanf("%s",dic[i]);
70         printf("%d\n",solve());
71     }
72     return 0;
73 }

 

    

POJ3267——The Cow Lexicon(动态规划)

标签:des   style   blog   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/Enumz/p/4095070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!