[USACO2007FEB S] The Cow Lexicon S
题目描述
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的更多相关文章
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- The Cow Lexicon
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...
- HDOJ-三部曲-1015-The Cow Lexicon
The Cow Lexicon Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单 ...
随机推荐
- 带你上手基于Pytorch和Transformers的中文NLP训练框架
本文分享自华为云社区<全套解决方案:基于pytorch.transformers的中文NLP训练框架,支持大模型训练和文本生成,快速上手,海量训练数据>,作者: 汀丶 . 1.简介 目标: ...
- 商品详情api接口的应用方向有哪些?
商品详情API接口的应用方向非常广泛,可以应用于以下领域: 电子商务平台:商品详情API接口可以提供商品的基本信息,如名称.描述.价格.图片等,帮助电子商务平台展示和推荐商品.此外,还可以提供商品 ...
- Codeforces Round 882 div.2 A
Smiling&Weeping ----总有人间一两风,填我十万八千梦 A. The Man who became a God time limit per test 1 second mem ...
- 如何理解SpringBoot的Starter
Starter是SpringBoot的四大核心功能特性之一,除此之外,SpringBoot还有自动装配,Actuator监控等特性 SpringBoot里面的这些特性,都是为了让开发者在开发基于Spr ...
- Web攻防--Java_SQL注入--XXE注入-- SSTI模板注入--SPEL表达式注入
预编译 编译器在编译sql语句时,会依次进行词法分析.语法分析.语义分析等操作, 预编译技术会让数据库跳过编译阶段,也就无法就进行词法分析,关键字不会被拆开,注入语句也就不会被识别为SQL的关键字,从 ...
- 面试题:Mybatis中的#{}和${}有什么区别?这是我见过最好的回答
面试题:Mybatis中的#{}和${}有什么区别? 前言 今天来分享一道比较好的面试题,"Mybatis中的#{}和${}有什么区别?". 对于这个问题,我们一起看看考察点和比较 ...
- 《微服务架构设计》——Eventuate Tram框架订阅/消费模式源码解析
Eventuate Tram框架官方文档: https://eventuate.io/docs/manual/eventuate-tram/latest/getting-started-eventua ...
- Python ChatGPT Telegram Bot
注册 这里如何注册我就不说明了,大家自行去注册,主要是现在GPT的基本上已经备用很多了,导致了接码的价格也上涨了,而且使用token的话,其实还是很快可以用完免费的18美金: 接码:https://s ...
- Go协程揭秘:轻量、并发与性能的完美结合
Go协程为并发编程提供了强大的工具,结合轻量级.高效的特点,为开发者带来了独特的编程体验.本文深入探讨了Go协程的基本原理.同步机制.高级用法及其性能与最佳实践,旨在为读者提供全面.深入的理解和应用指 ...
- Goobye, cnblogs
转 typecho 了,个人网站的客制化程度当然不是 cnblogs 能比得上的. <cirnovsky.cf>