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. wampserver,eclipse,PHPeclipse搭建php开发环境

    使用wampserver,安装wampserver后: 1,)进入mysql console,修改mysql密码: 2,) 修改D:\wamp\apps\phpmyadmin3.4.10.1\conf ...

  2. eclipse Project facet Java version 1.8 is not supported.

    在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的, ...

  3. PostgreSQL Replication之第十三章 使用PL/Proxy扩展(3)

    13.3 聪明地扩展与处理集群 建立集群不是您面临的唯一任务.如果所有的事情都做完了并且系统已经运行了,您可能需要到处调整配置. 13.3.1 添加和移动分区 一旦一个集群启动并运行,您可能会发现您的 ...

  4. Java基础(1):Switch语句注意的5个地方

    不得不说的几点小秘密: 1. switch 后面小括号中表达式的值必须是整型或字符型 2. case 后面的值可以是常量数值,如 1.2:也可以是一个常量表达式,如 2+2 :但不能是变量或带有变量的 ...

  5. java读properties的通用类,兼容linux和windows

    package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...

  6. Android 利用Service BroadcastReceiver实现小例子

    Activity: package com.example.test; import android.app.Activity; import android.content.Context; imp ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)

    我们刚才只是完成了添加的一部分,但是我们的上级分类也不能永远都是只有一个死的嘛,所以我们需要对她进行修改: 我们先将add方法里面的数据查出来再说: 然后在模板页进行遍历: 展示效果如下所示: 虽然是 ...

  8. js默认比较第一个数字大小

    解决办法:先把数字用Number转化:num=Number(num);然后再比较 和parseInt区别

  9. 1.js基础

    1.如何在html文档中使用js 1)使用<script></script>将JS语法嵌入到html中,可以使用多个,每个之间都是有关联的 2)href="javas ...

  10. Sqlserver Sql Agent Job 只能同时有一个实例运行

    Sqlserver Sql Agent中的Job默认情况下只能有一个实例在运行,也就是说假如你的Sql Agent里面有一个正在运行的Job叫"Test Job",如果你现在再去启 ...