题目描述

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.

输入格式

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

输出格式

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

样例输入 #1

6 10
browndcodw
cow
milk
white
black
brown
farmer

样例输出 #1

2

首先考虑暴力。设计\(dp_i\)表示前i个字符要去掉多少个字符可以被翻译。暴力转移就是枚举上一个单词的结尾在j,在j+1~i中只能有一个单词,那么价值是\((i-j)-\)最长的单词。不妨枚举选了哪一种单词,然后看一下能不能匹配。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int w,l,dp[N],len[N];
char s[N][N],t[N];
int can(int x,int y)
{
int ans=y-x+1;
for(int k=1;k<=w;k++)//枚举选哪一种单词
{
int r=1;
for(int i=x;i<=y;i++)
{
if(s[k][r]==t[i])
++r;
if(r>len[k])//可以匹配上
ans=min(ans,y-x+1-len[k]);
}
}
return ans;
}
int main()
{
scanf("%d%d%s",&w,&l,t+1);
for(int i=0;i<=l;i++)
dp[i]=i;
for(int i=1;i<=w;i++)
scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
for(int i=1;i<=l;i++)
for(int j=0;j<i;j++)
dp[i]=min(dp[i],dp[j]+can(j+1,i));
printf("%d",dp[l]);
return 0;
}

但是会超时获得70分。我们发现可以预处理出每个can(l,r)。枚举l和选哪种单词,然后往后推r,如果一个单词在某一处可以被匹配那么在后面都可以匹配的上。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int w,l,dp[N],len[N],f[N][N];
char s[N][N],t[N];
int can(int x,int y)
{
int ans=2e9;
for(int k=1;k<=w;k++)
{
int r=1;
for(int i=x;i<=y;i++)
{
if(s[k][r]==t[i])
++r;
if(r>len[k])
ans=min(ans,y-x+1-len[k]);
}
}
return ans;
}
int main()
{
memset(f,0x7f,sizeof(f));
scanf("%d%d%s",&w,&l,t+1);
for(int i=0;i<=l;i++)
dp[i]=i;
for(int i=1;i<=w;i++)
scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
// return can(6,10);
for(int i=1;i<=l;i++)
{
int ans=2e9;
for(int k=1;k<=w;k++)
{
int r=1;
for(int j=i;j<=l;j++)
{
if(s[k][r]==t[j])
++r;
if(r>len[k])
f[i][j]=min(f[i][j],j-i+1-len[k]);
}
}
}
for(int i=1;i<=l;i++)
for(int j=0;j<i;j++)
dp[i]=min(dp[i],dp[j]+f[j+1][i]);
printf("%d",dp[l]);
return 0;
}

[USACO2007FEB S] The Cow Lexicon S的更多相关文章

  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. The Cow Lexicon

    The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...

  5. HDOJ-三部曲-1015-The Cow Lexicon

    The Cow Lexicon Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  6. POJ3267——The Cow Lexicon(动态规划)

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  7. poj3267--The Cow Lexicon(dp:字符串组合)

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

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

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

  9. POJ 3267-The Cow Lexicon(DP)

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

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

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

随机推荐

  1. docker-compose快速部署elasticsearch-8.x集群+kibana

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<Docker下elasticse ...

  2. 《CTFshow-Web入门》05. Web 41~50

    @ 目录 web41 题解 原理 web42 题解 原理 web43 题解 原理 web44 题解 原理 web45 题解 原理 web46 题解 原理 web47 题解 web48 题解 web49 ...

  3. 【Python进阶-PyQt5】00PyQt5简介

    0.图形用户界面-开发选择 在Python基础的教程中,我们程序的用户交互界面都是运行窗口.这个运行窗口对于我们编程者来说直观明了,但是对于一些相对复杂的程序,用户使用上就会变得十分麻烦.所以,我们要 ...

  4. SQL多表查询指南

    SQL多表查询指南 在实际的数据库应用中,通常需要查询涉及多个表的数据.SQL提供了多种方法来执行这种多表查询操作. 内连接(INNER JOIN) 内连接是将多个表中满足连接条件的行组合在一起的操作 ...

  5. PYQT5学习(12)Qtabwidget 选项卡及其窗口,Qstackedwidget和Qtabwidget的效果类似,以及系统托盘QsystemtrayIcon

    参考博文:https://blog.csdn.net/jia666666/article/details/81669092QTabWidget控件提供一个选项卡和一个页面区域,默认显示第一个选项卡的页 ...

  6. 6-MySQL查询条件

    在MySQL中,高级查询是指使用更复杂的查询语句和操作符来检索和操作数据库中的数据.高级查询可以帮助您更精确地找到所需的信息,并提高查询的效率和灵活性. 以下是高级查询的一些常见应用场景和意义: 连接 ...

  7. SpringBootAdmin_监控

    监控的意义 监控服务状态是否宕机 监控服务运行指标(内存.虚拟机.线程.请求等) 监控日志 管理服务(服务下线) 监控的实施方式 大部分监控平台都是主动拉取监控信息,而不是被动地等待应用程序传递信息 ...

  8. ⭐volatile⭐ 用volatile关键字则会从内存中直接读取变量的值

  9. AOP(面向切面编程)

    什么是AOP AOP(Aspect Oriented Programming,面向切面编程),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP的作用 利用AOP编程可以对业务逻 ...

  10. Java21上手体验-分代ZGC和虚拟线程

    一.导语 几天前Oracle刚刚发布了Java21, 由于这是最新的LTS版本,引起了大家的关注. 我也第一时间在个人项目中进行了升级体验. 一探究竟,和大家分享. 二.Java21更新内容介绍 官方 ...