The Cow Lexicon

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 8815 Accepted: 4162

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

Source

USACO 2007 February Silver

求主串与字典之间匹配要删除的字符数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 20001; int dp[400];//记录从i到L之间要匹配字典要删除的字符数目 char s[400]; char str[650][400]; int len[650]; int main()
{
int n,L;
while(~scanf("%d %d",&n,&L))
{
scanf("%s",s);
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
len[i]=strlen(str[i]);
}
memset(dp,0,sizeof(dp));
for(int i=L-1;i>=0;i--)
{
dp[i]=dp[i+1]+1;//最坏的情况,将字符删除
for(int j=0;j<n;j++)//将i到L之间的字符与字典进行匹配
{
if(str[j][0]==s[i]&&i+len[j]<=L)
{
int ss=i;
int t=0;
while(ss<L)
{
if(str[j][t]==s[ss++])
{
t++;
}
if(t==len[j])//如果能够匹配,计算最小值
{
dp[i]=min(dp[i],dp[ss]+ss-i-len[j]);//对于ss,ss到L之间的匹配是已经算出来了,dp[ss]+ss-i-len[j]表示i到L之间匹配要删除的字符数,求最小值
break;
}
}
}
}
}
printf("%d\n",dp[0]);
} return 0;
}

The Cow Lexicon的更多相关文章

  1. POJ3267 The Cow Lexicon(DP+删词)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9041   Accepted: 4293 D ...

  2. POJ 3267 The Cow Lexicon

    又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...

  3. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  4. HDOJ-三部曲-1015-The Cow Lexicon

    The Cow Lexicon Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

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

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  6. poj3267--The Cow Lexicon(dp:字符串组合)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8211   Accepted: 3864 D ...

  7. BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典

    题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 401  Solv ...

  8. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  9. bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单 ...

随机推荐

  1. 数据库执行监控,除了Profiler的方案

    怎么查找库中的数据库相关信息: Select * from sys.databases; 返回字段: name(数据库名称) database_id(数据库id) .... Select sqlTex ...

  2. iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“

    需要你写一下你确认提交的信息,就是你这次提交上去修改了什么功能,简单描述一下

  3. chat

    启动服务器 连接数据库 导入数据库并显示 开启监听 与客户端建立连接 写入数据库并显示 创建线程用来接受客户端消息

  4. EJS 是什么 ,怎么用,以及优点

    一.什么是EJS EJS是一个JavaScript模板库,用来从JSON数据中生成HTML字符串. 二.为什么要使用EJS 与最初的JavaScript相比较,一些不太了解你的代码的人可以更容易地通过 ...

  5. 每天一个liunx命令(ubuntu)

    解压XXX.gz到另一个A文化夹: 1.切换到root权限 su 2.tar -zxvf  XXX.gz -C  A 注意: C要大些因为ubuntu区分大小写

  6. jQuery中处理事件冒泡的方法和取消后续内容的方法

    一:事件冒泡的意思是:一个大的容器已经设置了事件,如果这个容器里还包容着一个小的容器也设置了自己的事件,那么因为小容器是在大容器里面的,触发小容器的事件同时也等于触发了大容器的事件,有时这并不是我们想 ...

  7. bzoj1834 [ZJOI2010]network 网络扩容

    第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...

  8. openssh设置免密码登录远程服务器

    主机 host | 远程机器 server 目标:从host登录到server,免密码输入 host机器: ssh-keygen -t rsa 在~/.ssh/中会生成两个文件 id_rsa.pub和 ...

  9. 夺命雷公狗---微信开发54----微信js-sdk接口开发(1)之快速入门

    js-sdk基本介绍 除去服务号的九大接口外,微信提供了JS-SDK接口,所谓JS-SDK接口也就是在网页中使用javascript来更改网页设置, (比如隐藏右上角的菜单)获取用户状态(比如地理位置 ...

  10. PAT乙级 1009. 说反话 (20)

    1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...