[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$个单 ...
随机推荐
- 【io_uring】简介和使用
文章目录 简介 使用 系统调用 liburing 样例 代码流程 编译 参考资料 简介 io_uring 是 Linux 在 5.1 版本引入的一套新的异步 IO 实现.相比 Linux 在 2.6 ...
- jQuery Ajax执行顺序问题
代码如下: $(document).ready(function () { var res; $.ajax({ type: 'post', url: 'GridDemo.aspx/PlaceOrder ...
- C# API复制/拷贝到剪辑板
备忘 昨天在做一个程序的时候需要用到"剪辑板"功能, 可是死活引用不了"windows.forms"- (忘记添加引用了) 无奈只好去找了一个易语言的" ...
- MFAN论文阅读笔记(待复现)
论文标题:MFAN: Multi-modal Feature-enhanced Attention Networks for Rumor Detection 论文作者:Jiaqi Zheng, Xi ...
- [MAUI]实现动态拖拽排序列表
@ 目录 创建页面元素 创建可绑定对象 创建绑定服务类 拖拽(Drag) 拖拽悬停,经过(DragOver) 释放(Drop) 限流(Throttle)和防抖(Debounce) 项目地址 上一章我们 ...
- 一个颜值功能双在线的Zookeeper可视化工具
大家好,我是 Java陈序员,今天给大家介绍一个颜值功能双在线的 Zookeeper 可视化工具. 项目介绍 PrettyZoo 是一个基于 Apache Curator 和 JavaFX 实现的 Z ...
- windows上U盘格式化失败提示系统找不到指定文件
某天同事拿来几个U盘,问需不需要,我随便看了眼还挺新的,于是插上电脑看看能否正常使用,果然无法识别,因为没有使用需求了也就放着没管了. 突然有一天要去客户现场搞私有化交付了,自己带物料,这下就派上用场 ...
- 使用playwright爬取魔笔小说网站并下载轻小说资源
一.安装python 官网 下载python3.9及以上版本 二.安装playwright playwright是微软公司2020年初发布的新一代自动化测试工具,相较于目前最常用的Selenium,它 ...
- Fortran 的简单入门和使用 OpenMPI
Fortran 与 C-like 语言的区别简单总结 无大括号,使用关键字画出范围: C++: int main() { } Fortran: program test implicit none e ...
- java算法之排序算法大全
①排序 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法.排序算法在很多领域得到相当地重视,尤其是在大量数据的处理方 ...