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. ubuntu16.04+matlab r2015b VideoReader报错

    读取.mp4出错 需要安装gstreamer0.10-ffmpeg ` sudo add-apt-repository ppa:mc3man/gstffmpeg-keep sudo apt-get u ...

  2. eclipse 项目转as项目时 .so文件 jniLibs的设置

    接着上篇:http://blog.csdn.net/u011644423/article/details/46989167 转换项目 还是出现了问题 java.lang.UnsatisfiedLink ...

  3. bootstrap使用总结

    bootstrap是一个webcss框架,集合了html/css/jquery为一家,创建响应式的页面.所谓的响应式就是适配不同的上网设备. 使用bootstrap的步骤: 1.下载bootstrap ...

  4. 解决Shiro+SpringBoot自定义Filter不生效问题

    在SpringBoot+Shiro实现安全框架的时候,自定义扩展了一些Filter,并注册到ShiroFilter,但是运行的时候发现总是在ShiroFilter之前就进入了自定义Filter,结果当 ...

  5. 虚拟机VMware中的CentOS字符界面和图形界面切换

    在虚拟机中安装CentOS后展示的界面是图形用户界面,想切换到命令模式,于是查了linux下切换模式的方法,可是按了ctrl+alt+f1.f2....f7都是没用,后来发现是因为按键冲突的原因 问题 ...

  6. [转帖]SQLSERVER的兼容级别

    SQL Server数据库的兼容级别 http://www.cnblogs.com/sosoft/archive/2017/07/08/sqljrjb.html 改天尝试一下 在SQLSERVER20 ...

  7. JVM内存模型一

    JVM定义了若干个程序执行期间使用的数据区域.这个区域里的一些数据在JVM启动的时候创建,在JVM退出的时候销毁.而其他的数据依赖于每一个线程,在线程创建时创建,在线程退出时销毁. 程序计数器 程序计 ...

  8. spring+struts整合

    首先是为什么整合strut2和spring? struts2的action是由struts2自己创建出来的,它不受spring的委托,也不受spring的管理,所以无法进行自动注入:spring和st ...

  9. 【刷题】BZOJ 2693 jzptab

    Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的结果 Sample Input 1 4 5 Sa ...

  10. 响应式开发(二)-----Bootstrap框架的介绍

    简介 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架,是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的, ...