[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 ...
随机推荐
- http 连接失败重连机制
1.我们做web开发时,需要经常使用httpclient来请求http服务,有时为了安全起见,服务提供方会提供多个http地址,这样如果我们请求某个ip出现异常,可以重试其他的ip地址,来尽量保证系统 ...
- EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..
在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...
- Office WORD如何输入长下划线
选中一段文字,点击下划线按钮,可以添加下划线 同样,选中一段空格,点下划线,也可以添加下划线
- JMeter 系列之—-01使用
用Jmeter 做压测,总体与LoadRunner 类似: 一.线程组 1. 线程数 2. 循环次数 单个线程循环次数 3. Ramp-up Period(in seconds) [1]决定多长时间启 ...
- [LeetCode][Java] Remove Duplicates from Sorted List II
题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- iOS 开发者中的个人账号与组织账号之间区别
苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...
- Java类加载机制?
深入研究Java类加载机制 类加载是Java程序运行的第一步,研究类的加载有助于了解JVM执行过程,并指导开发者采取更有效的措施配合程序执行. 研究类加载机制的第二个目的是让程序能动态的控制类加载,比 ...
- 相关性系数缺点与证明 k阶矩
相关性系数 https://baike.baidu.com/item/相关系数/3109424?fr=aladdin 缺点 需要指出的是,相关系数有一个明显的缺点,即它接近于1的程度与数据组数n相关, ...
- 使用PXE安装CentOS7
1.环境 本文使用VMware 虚拟机进行实验. 点击VMware--编辑--虚拟网络编辑器,新建VMnet15,选择仅主机模式,取消勾选DHCP服务(因为这里使用自己的DHCP服务).我这里配好后是 ...
- 解耦与分离 —— 面向切面编程(AOP)
家里的电表总结起来有两大特性: 电视机需要(电量管理),空调需要(电量管理),热水器也需要电量管理,即一组对象都需要某一功能特性: 电视机根据信号输出画面,空调吹出冷风,热水器将水加热,这些业务功能的 ...