[USACO 2011 Dec Gold] Threatening Letter【后缀】
Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and wants to send him
a nasty letter, but wants to remain anonymous. As so many before
him have done, he plans to cut out printed letters and paste them
onto a sheet of paper. He has an infinite number of the most recent
issue of the Moo York Times that has N (1 <= N <= 50,000) uppercase
letters laid out in a long string (though read in as a series of
shorter strings). Likewise, he has a message he'd like to compose
that is a single long string of letters but that is read in as a
set of shorter strings. Being lazy, he wants to make the smallest possible number of cuts.
FJ has a really great set of scissors that enables him to remove
any single-line snippet from the Moo York Times with one cut. He
notices that he can cut entire words or phrases with a single cut,
thus reducing his total number of cuts. What is the minimum amount of cuts he has to make to construct his
letter of M (1 <= M <= 50,000) letters? It is guaranteed that it is possible for FJ to complete his task. Consider a 38 letter Moo York Times: THEQUICKBROWNFOXDO
GJUMPSOVERTHELAZYDOG from which FJ wants to construct a 9 letter message: FOXDOG
DOG These input lines represent a pair of strings: THEQUICKBROWNFOXDOGJUMPSOVERTHELAZYDOG
FOXDOGDOG Since "FOXDOG" exists in the newspaper, FJ can cut this piece out
and then get the last "DOG" by cutting out either instance of the
word "DOG". Thus, he requires but two cuts. PROBLEM NAME: letter INPUT FORMAT: * Line 1: Two space-separated integers: N and M * Lines 2..?: N letters laid out on several input lines; this is the
text of the one copy of the Moo York Times. Each line will
have no more than 80 characters. * Lines ?..?: M letters that are the text of FJ's letter. Each line
will have no more than 80 characters. SAMPLE INPUT (file letter.in): 38 9
THEQUICKBROWNFOXDO
GJUMPSOVERTHELAZYDOG
FOXDOG
DOG OUTPUT FORMAT: * Line 1: The minimum number of cuts FJ has to make to create his
message SAMPLE OUTPUT (file letter.out): 2
一看跟子串相关,就是后缀那一套了。想法是这样的,尽量在文本串中找到更长的子串与当前串匹配。若无法继续匹配了,则重新匹配,答案+1。这里我选择了后缀自动机,实现起来好写。
#include <cstdio>
#include <cstring> const int maxn = 50005; int n, m, ans, now;
int sam[maxn << 1][26], len[maxn << 1], link[maxn << 1], sz, last, p, q, cur, clone;
char ch; int main(void) {
freopen("letter.in", "r", stdin);
freopen("letter.out", "w", stdout);
scanf("%d%d", &n, &m);
link[sz++] = -1;
for (int i = 1; i <= n; ++i) {
while ((ch = getchar()) < 'A');
cur = sz++;
len[cur] = len[last] + 1;
for (p = last; p != -1 && !sam[p][ch - 'A']; p = link[p]) {
sam[p][ch - 'A'] = cur;
}
if (p != -1) {
q = sam[p][ch - 'A'];
if (len[p] + 1 == len[q]) {
link[cur] = q;
}
else {
clone = sz++;
memcpy(sam[clone], sam[q], sizeof sam[0]);
link[clone] = link[q];
len[clone] = len[p] + 1;
for (; p != -1 && sam[p][ch - 'A'] == q; p = link[p]) {
sam[p][ch - 'A'] = clone;
}
link[q] = link[cur] = clone;
}
}
last = cur;
} for (int i = 1; i <= m; ++i) {
while ((ch = getchar()) < 'A');
now = sam[now][ch - 'A'];
if (!now) {
++ans;
now = sam[0][ch - 'A'];
}
}
if (now) {
++ans;
}
printf("%d\n", ans);
return 0;
}
这也算是SAM的模版了叭。
[USACO 2011 Dec Gold] Threatening Letter【后缀】的更多相关文章
- [USACO 2011 Dec Gold] Cow Calisthenics【二分】
Problem 1: Cow Calisthenics [Michael Cohen, 2010] Farmer John continues his never-ending quest to ke ...
- [USACO 2017 Dec Gold] Tutorial
Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...
- [USACO 2011 Nov Gold] Cow Steeplechase【二分图】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...
- [USACO 2011 Nov Gold] Above the Median【逆序对】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...
- [Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]
和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k #include <iostream> #include <algorithm> ...
- Usaco 2010 Dec Gold Exercise(奶牛健美操)
/*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...
- BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- BZOJ1775[USACO 2009 Dec Gold 3.Video Game Troubles]——DP
题目描述 输入 * 第1行: 两个由空格隔开的整数: N和V * 第2到第N+1行: 第i+1行表示第i种游戏平台的价格和可以在这种游戏平台上面运行的游 戏.包含: P_i, G_i还有G_i对由空格 ...
- [USACO 2016 Dec Gold] Tutorial
Link: 传送门 A: 贪心从小到大插入,用并查集维护连通性 #include <bits/stdc++.h> using namespace std; #define X first ...
随机推荐
- python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用
1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...
- 【C++基础 02】深拷贝和浅拷贝
我的主题是.每天积累一点点. =========================================== 在类定义中,假设没有提供自己的拷贝构造函数,则C++提供一个默认拷贝构造函数. C ...
- 【python】Python的字典get方法:从字典中获取一个值
转自: http://blog.sina.com.cn/s/blog_6be89284010183xm.html
- springMVC4(16)拦截器解析与登陆拦截模拟
在SpringMVC中,我们会常常使用到拦截器,尽管SpringAOP也能帮我们实现强大的拦截器功能,但在Web资源供给上.却没有SpringMVC来得方便快捷. 使用SpringMVC拦截器的核心应 ...
- 关键路径(AOE)---《数据结构》严蔚敏
// exam1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- Robot Framework操作
Robot Framework 介绍 RobotFramework是一款基于python的开源自动化测试框架,遵守Apache License 2.0协议,在此协议下所有人都可以免费开发和使用.因为R ...
- linux系列之-—01 shell编程笔记
一.特殊变量($0.$1.$2. $?. $# .$@. $*) shell编程中有一些特殊的变量可以使用.这些变量在脚本中可以作为全局变量来使用. 名称 说明 $0 脚本名称 $1-9 脚本执行时的 ...
- 通过通过url routing解决UIViewController跳转依赖
XYRouter https://github.com/uxyheaven/XYRouter XYRouter是一个通过url routing来解决UIViewController跳转依赖的类. * ...
- 多媒体开发之wis-stream
在live555的mediaServer中,已经实现RTSP-over-HTTP,但默认没有开启.如果要实现这个功能,需要调用RTSPServer::setUpTunnelingOverHTTP(), ...
- LEA指令与MOV指令的区别——发现一本汇编好书
一.汇编语言中PTR的含义及作用mov ax,bx ;是把BX寄存器“里”的值赋予AX,由于二者都是word型,所以没有必要加“WORD”mov ax,word ptr [bx];是把内存地址等于“B ...