poj3267

The Cow Lexicon

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5619 Accepted: 2599

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
这个题目还是跟最长公共子序列类似,
状态表示还是用f[i]表示前i个中最少去掉的数目能够构成合法序列
则f[i]=i
f[i]=min(f[i],f[j]+remove(s[j+1..i],ss[k]))
if ss[k]能在s[j+1..i]中表示出来
所以我们不必用LCS来求这个remove,直接用pp[k]表示第k个单词能在s[j+1..i]中匹配到的地方的地方
如果单词能在其中表示出来,则min
这类题目的类似在于f[i]总是由f[j](0<j<i)推出来的
 1#include<stdio.h>
 2#include<string.h>
 3#include<math.h>
 4#define MAX 350
 5int f[MAX];
 6char sd[MAX];
 7int pp[610],len[610];
 8char s[610][26];
 9int l1,n,i,j,k;
10int min(int a,int b)
11{
12    if (a>b) return b;
13    else return a;
14}

15int main()
16{
17    scanf("%d%d",&n,&l1);
18    scanf("%s",&sd);
19    for (i=l1-1;i>=0 ;i-- )
20    {
21        sd[i+1]=sd[i];
22    }

23    for (i=1; i<=n ; i++ )
24    {
25        scanf("%s",&s[i]);
26        len[i]=strlen(s[i]);
27    }

28    for (i=1; i<=l1 ; i++ )
29    {
30        f[i]=i;
31        for (j=1; j<=n ; j++)
32        {
33            pp[j]=len[j]-1;
34        }

35        for (j=i; j>=1; j--)
36        {
37            for (k=1; k<=n ; k++ )
38            {
39                if (sd[j]==s[k][pp[k]])
40                {
41                    pp[k]--;
42                }

43                if (pp[k]<0)
44                {
45                    f[i]=min(f[i],f[j-1]+i-j-len[k]+1);
46                }

47            }

48        }

49    }

50    printf("%d\n",f[l1]);
51    return 0;
52}

53//f[i]=min(f[j]+remove(s[j+1..i],s[k]))
54//
55
 

posted on 2012-02-21 19:31 jh818012 阅读(392) 评论(1)  编辑 收藏 引用

评论

# re: poj3267 2012-02-29 17:23 王私江

我嚓,我该努力了。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论