题目描述

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

说明

感谢@ws_fuweidong 提供完整题面

思路

题意:计算至少需要删除多少个字母才能使该序列正好由给出的单词组成

设dp[i]代表的是从i~Len这部分序列中最少所要删除的单词数,初始化dp[Len]=0。状态转移方程如下:

$$dp[i]=dp[i+1]+1 (不匹配,直接删)$$
$$dp[i]=min(dp[i],dp[p1]+(p1-i)-len) (能的话就取最优)$$

理解:

第一个很好理解,就是在不能匹配的情况下,在上一个的基础上多删除一个,也就是删除新加入的单词。

第二个有点复杂,len就是单词的长度,p1相当于是指向序列的指针,p1-i代表的是包含当前单词的序列的长度,因为当中可能还掺杂着别的一些单词,p1-i-len代表的就是多余单词的个数,也就是需要删除个数(比如说,当前序列为codw,比较单词为cow,p1-i=4,p1-i-len=1,也就是要删除d这一个多余的单词)。dp[p1]显然就是序列为p1~Len时最少所要删除的单词数。

代码

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
int x=0,w=1;
char s=getchar();
while(s!='-'&&(s<'0'||s>'9')) s=getchar();
if(s=='-') w=-1,s=getchar();
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+s-48,s=getchar();
return x*w;
}
char words[605][30];
char s[305];
int dp[305]; int main() {
int W, L;
cin>>W>>L;
cin>>s;
for(re i=0;i<W;i++) cin>>words[i];
dp[L]=0;
for(re i=L-1;i>=0;i--) {
dp[i]=dp[i+1]+1; //无法匹配时需要删除的字符数,先记录一下最坏情况
for (int j=0;j<W;j++) {
int len=strlen(words[j]);
if (len<=L-i&&words[j][0]==s[i]) { //单词长度不能大于i~L字段的长度并且首字母得相同
int p1=i;
int p2=0;
while(p1<L){
if (s[p1]==words[j][p2]) {
p1++;
p2++;
} else p1++;
if (p2==len) {
dp[i]=min(dp[i],dp[p1]+p1-i-len);
break;
}
}
}
}
}
cout << dp[0] << endl;
return 0;
}

c++

【题解】Luogu P2875 [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. [USACO07FEB]牛的词汇The Cow Lexicon

    https://daniu.luogu.org/problemnew/show/P2875 dp[i]表示前i-1个字符,最少删除多少个 枚举位置i, 如果打算从i开始匹配, 枚举单词j,计算从i开始 ...

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

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

  5. 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...

  6. luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  7. LuoGu P2863 [USACO06JAN]牛的舞会The Cow Prom

    题目传送门 这个题还是个缩点的板子题...... 答案就是size大于1的强连通分量的个数 加一个size来统计就好了 #include <iostream> #include <c ...

  8. 题解 P2863 【[USACO06JAN]牛的舞会The Cow Prom】

    题目链接 赤裸裸的板子,就加一个特判就行.直接上代码 #include<stdio.h> #include<algorithm> #include<iostream> ...

  9. [题解] Luogu P5446 [THUPC2018]绿绿和串串

    [题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...

随机推荐

  1. Linux下查看在线用户及用户进程

    #该服务器下的所有用户运行进程的情况 ps -ax -u #查看java程序下用户的进程情况 ps -ax -u |grep java   或  ps aux|grep java cat /etc/p ...

  2. IP子网划分与聚合

    一:IP地址: IP地址是由32位2进制数组成,每8位一组.由点分十进制表达. IP地址可以分为五类 A类(1.0.0.0-126.255.255.255),127.0.0.1 为本地回环地址. B类 ...

  3. 面试侃集合 | LinkedBlockingQueue篇

    面试官:好了,聊完了ArrayBlockingQueue,我们接着说说LinkedBlockingQueue吧 Hydra:还真是不给人喘口气的机会,LinkedBlockingQueue是一个基于链 ...

  4. ESLint语法报错问题

    编写javaScript过程中ESLint语法报错问题 ESLint语法要求: 双引号""需要替换成单引号'' 分号不允许出现 ()之前需要一个空格比如 login () (VSC ...

  5. [刷题] PTA 查验身份证

    题目: 7-63 查验身份证 (15 分)  一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...

  6. echo -n -e "请输入重启间隔的时间(分钟):\t"

    echo -n -e "请输入重启间隔的时间(分钟):\t"read interval##echo -n "Your choice is " # 加上 -n 可 ...

  7. 006.Ansible自定义变量

    ansible支持变量,用于存储会在整个项目中重复使用到的一些值.以简化项目的创建与维护,降低出错的机率. 变量的定义: 变量名应该由字母.数字下划数组成 变量名必须以字母开头 ansible内置关键 ...

  8. 063.Python前端Django分页器

    Django的分页器 1 前期准备 创建一个数据库,用于存放数据 mysql> create database pager default charset=utf8; mysql> use ...

  9. 3.socket编程示例

    #block_server.py 非阻塞IO示例#有个疑惑:下面的connfd的blockind要设置为True,不然会出错,待解决from socket import *from time impo ...

  10. Centos7 离线安装python3 Django

    安装python 1..下载Python3源码包 下载地址:www.python.org/ftp/python/ 2.安装python前的库环境 yum install gcc patch libff ...