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. Sql Server中判断表、列不存在则创建的方法[转]

    一.Sql Server中如何判断表中某列是否存在 首先跟大家分享Sql Server中判断表中某列是否存在的两个方法,方法示例如下: 比如说要判断表A中的字段C是否存在两个方法: 第一种方法  ? ...

  2. modify headers插件的使用

    Modity headers是firefox浏览器的一个插件,作用是改变http请求的IP地址 (一)在firefox中添加该插件 步骤一:打开firefox浏览器,打开地址: https://add ...

  3. 【vue】import的使用

    以下是vue默认模板结构,自动加载HelloWorld (1)@ 等价于 /src 这个目录,避免写麻烦又易错的相对路径,是在webpack.base.config.js里面配置好别名 (2)impo ...

  4. zoj 2588 Burning Bridges(割边/桥)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...

  5. 清理elasticsearch的索引

    curl -XDELETE 'http://172.16.1.16:9200/logstash-2013.03.*' 清理掉了所有 3月份的索引文件,其中*是通配符 下面是主页上的详细介绍,其他部分可 ...

  6. Eclipse HTML Editor

    需插件: 1.GEF 3.1 安装程序下载 下载地址: http://download.eclipse.org/tools/gef/downloads/drops/R-3.1-200507071758 ...

  7. Android CollapsingToolbarLayout

    第一次看到这种用户体验是在Google Play Store App的应用详情的Activity中. 大的Banner图,能第一时间吸引用户的眼球,用不一样的Banner大图更具个性化的展示内容.图总 ...

  8. QML——添加自定义模块

    一.模块的定义 在使用QML时,我们常常看到类似这样的语句: import QtQuick 2.0 import QtSensors 5.0 通过这些import语句,我们就能使用QML中的相关控件了 ...

  9. Java将其他数据类型转换成JSON字符串格式

    Student.java package com.demo.servlet; import java.util.List; import java.util.Map; public class Stu ...

  10. C++中三种传递参数方法的效率分析

    众所周知,在C++中有三种参数传递的方式: 按值传递(pass by value) #include <iostream> using namespace std; void swap(i ...