https://daniu.luogu.org/problemnew/show/P2875

dp[i]表示前i-1个字符,最少删除多少个

枚举位置i,

如果打算从i开始匹配,

  枚举单词j,计算从i开始往后,匹配完单词j的位置pos,删除的字母个数sum

    dp[pos]=min(dp[i]+sum)

如果不用i匹配,dp[i+1]=min(dp[i])

#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; #define N 301 int l; char s[N];
char word[][];
int len[]; int dp[N+]; void cal(int pos,int wh)
{
int sum=,tmp=dp[pos];
for(int i=;i<=len[wh];++i)
{
while(s[pos]!=word[wh][i])
{
pos++; sum++;
if(pos>l) return;
}
pos++;
}
dp[pos]=min(dp[pos],tmp+sum);
} int main()
{
int w;
scanf("%d%d",&w,&l);
scanf("%s",s+);
for(int i=;i<=w;++i)
{
scanf("%s",word[i]+);
len[i]=strlen(word[i]+);
}
int mi;
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=l;++i)
{
for(int j=;j<=w;++j)
if(i+len[j]-<=l)
if(s[i]==word[j][])
cal(i,j);
dp[i+]=min(dp[i]+,dp[i+]);
}
cout<<dp[l+];
}

题目描述

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.

没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如,贝茜听到有人对她说"browndcodw",确切的意思是"browncow",多出了两个"d",这两个"d"大概是身边的噪音.

奶牛们发觉辨认那些奇怪的信息很费劲,所以她们就想让你帮忙辨认一条收到的消息,即一个只包含小写字母且长度为L (2 ≤ L ≤ 300)的字符串.有些时候,这个字符串里会有多余的字母,你的任务就是找出最少去掉几个字母就可以使这个字符串变成准确的"牛语"(即奶牛字典中某些词的一个排列).

输入输出格式

输入格式:

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

• 第1行:两个用空格隔开的整数,W和L.

• 第2行:一个长度为L的字符串,表示收到的信息.

• 第3行至第W+2行:奶牛的字典,每行一个词.

输出格式:

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.

一个整数,表示最少去掉几个字母就可以使之变成准确的"牛语".

输入输出样例

输入样例#1: 复制

6 10
browndcodw
cow
milk
white
black
brown
farmer
输出样例#1: 复制

2

[USACO07FEB]牛的词汇The Cow Lexicon的更多相关文章

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

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

  2. 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...

  3. 【题解】Luogu P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    题目描述 Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no ...

  4. [luoguP2875] [USACO07FEB]牛的词汇The Cow Lexicon(DP)

    传送门 f[i] 表示前 i 个字符去掉多少个 的最优解 直接暴力DP ——代码 #include <cstdio> #include <cstring> #include & ...

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

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

  6. poj 3267 The Cow Lexicon (动态规划)

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

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

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

  8. POJ 3267 The Cow Lexicon

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

  9. POJ 3267:The Cow Lexicon(DP)

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

随机推荐

  1. 我是IT小小鸟读后感

    <我是一只IT小小鸟>一只是我想读list中一个本,但是上次去当当买的时候,竟然缺货了...昨天监考,实在无聊,就上网看电子书了,一天就看完了,看得有点仓促,所以理解估计不深. 1.刘帅: ...

  2. Teamwork(The fifth day of the team)

    在前面几天的努力中,我们已经完成了一些自己的工作,还有的就是一些完善,因为在前段时间一直都在寻找和配置Eclipse+Android SDK,由于版本和一些网络的阻碍,总是不能如愿的很好完成,经过了一 ...

  3. 初入React(一)

    React:是2013年Facebook在github上的一个开源js库,它将用户界面抽象为一个个组件,再由开发者将其组合成页面.它不是完整的MVC/MVVM框架,专注于提供清晰.简洁的view层解决 ...

  4. 封装GetQueryString()方法来获取URL的value值(转载)

    首先测试URL:http://192.168.1.82:8020/juzhong/daojishi.html?name=xiangruding&sex=nuuu&age=90 代码如下 ...

  5. javascript 组件化(转载)

    这边只是很简陋的实现了类的继承机制.如果对类的实现有兴趣可以参考我另一篇文章javascript oo实现 我们看下使用方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  6. python安装报错:Microsoft Visual C++ 14.0 is required

    保存详情如下: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build T ...

  7. c++ 替换修改一个文件夹下的所有文件的文件名

    代码简洁,亲测可用. 1,首先来获取(输出)一个文件夹中所有的文件名 void getFiles(string path, vector<string>& files) { //文 ...

  8. Spring Boot快速搭建Spring框架

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...

  9. BZOJ3270 博物馆(高斯消元+概率期望)

    将两个人各自所在点视为状态,新建一个图.到达某个终点的概率等于其期望次数.那么高斯消元即可. #include<iostream> #include<cstdio> #incl ...

  10. vyos 基础配置

    vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...