poj 3267 -- The Cow Lexicon

The Cow Lexicon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7987   Accepted: 3749

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]表示:第i个字符到最后 至少删除几个字符。

   则有: dp[i] = dp[i+1] + 1 匹配不到
      dp[i] = min(dp[i], dp[Tpoint] + Tpoint - i - len) 匹配到
  
   解释:Tpoint是在主串中的位置。len 是当前单词的长度。
      dp[Tpoint] 表示Tpoitn到最后应删除的字符数。 Tpoing-i-len 表示i到Tpoint 应删除的字符数。

 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   TheCowLexicon.cpp
 4  *       Creat time :   2014-07-12 15:06
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define N 605
15 using namespace std;
16 char str[N][N];
17 char T[N];
18 int dp[N];
19 int main(int argc,char *argv[])
20 {
21     int n,m;
22     while(scanf("%d%d",&n,&m)!=EOF){
23         getchar();
24         gets(T);
25         for(int i = 0; i < n; i++){
26             scanf("%s",str[i]);
27             getchar();
28         }
29         clr(dp,0);
30         for(int i = m-1; i >= 0; i--){
31             dp[i] = dp[i+1] + 1;
32             for(int j = 0; j < n; j++){
33                 int len = strlen(str[j]);
34                 if(m-i >= len && str[j][0] == T[i]){
35                     int strpoint = 0;
36                     for(int Tpoint = i; Tpoint < m;){
37                         if(str[j][strpoint] == T[Tpoint++]){
38                             strpoint++;
39                         }
40                         if(strpoint == len){
41                             dp[i] = min(dp[i],dp[Tpoint] + Tpoint-i-len);
42                             break;
43                         }
44                     }
45                 }
46             }
47         }
48         printf("%d\n",dp[0]);
49     }
50     return 0;
51 }
View Code

 

posted @ 2014-07-30 16:04  ZeroCode_1337  阅读(165)  评论(0编辑  收藏  举报