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【后缀】的更多相关文章

  1. [USACO 2011 Dec Gold] Cow Calisthenics【二分】

    Problem 1: Cow Calisthenics [Michael Cohen, 2010] Farmer John continues his never-ending quest to ke ...

  2. [USACO 2017 Dec Gold] Tutorial

    Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...

  3. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

  4. [USACO 2011 Nov Gold] Above the Median【逆序对】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...

  5. [Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]

    和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k #include <iostream> #include <algorithm> ...

  6. Usaco 2010 Dec Gold Exercise(奶牛健美操)

    /*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...

  7. BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd

    题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...

  8. 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对由空格 ...

  9. [USACO 2016 Dec Gold] Tutorial

    Link: 传送门 A: 贪心从小到大插入,用并查集维护连通性 #include <bits/stdc++.h> using namespace std; #define X first ...

随机推荐

  1. hdu1181 dfs搜索之变形课

    原题地址 这道题数据据说比較水,除了第一组数据是Yes以外.其余都是No.非常多人抓住这点就水过了.当然了,我认为那样过了也没什么意思.刷oj刷的是质量不是数量. 这道题从题目上来看是个不错的 搜索题 ...

  2. Mybatis加入Ehcache支持

    1.Mybatis默认的缓存配置 MyBatis 包括一个很强大的查询缓存特性,它能够很方便地配置和定制. Mybatis缓存包括全局的缓存和局部的缓存.全局的缓存能够讲主配置文件的setting属性 ...

  3. Windows环境下QWT安装及配置

    ** 1.QWT下载路径 **:https://sourceforge.net/projects/qwt/ 主要下载这三个文件:qwt-6.1.2.zip.qwt-6.1.2.pdf,qwt-6.1. ...

  4. HDU 1017 A Mathematical Curiosity【看懂题意+穷举法】

    //2014.10.17    01:19 //题意: //先输入一个数N,然后分块输入,每块输入每次2个数,n,m,直到n,m同一时候为零时  //结束,当a和b满足题目要求时那么这对a和b就是一组 ...

  5. 【翻译自mos文章】oracle db 中的用户账户被锁--查看oracle用户的尝试次数

    參考原文: Users Accounts Getting Locked. (Doc ID 791037.1) 事实上这个文章是为oracle 别的软件产品写的,只是涉及到user 锁定问题.那还是跟d ...

  6. scikit-learn:class and function reference(看看你究竟掌握了多少。。)

    http://scikit-learn.org/stable/modules/classes.html#module-sklearn.decomposition Reference This is t ...

  7. DLR之 ExpandoObject和DynamicObject的使用演示样例

    ExpandoObject :动态的增删一个对象的属性,在低层库(比如ORM)中非常实用.因为ExpandoObject实现了IDictionay<string, object>接口,常见 ...

  8. JS基本特效 -- 常见40个常用的js页面效果图(转载)

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu ...

  9. Hibernate 之 二级缓存

    在上篇文章中我们对缓存以及Hibernate的一级缓存进行了介绍,接下来的主要内容将是Hibernate的二级缓存. 二级缓存也称为进程级的缓存或SessionFactory级的缓存,二级缓存可以被所 ...

  10. Dos下同时执行多条命令简化操作

    起因,要查询8080端口被哪些程序占用了,但是直接在cmd netstat –ano 的话 一下子一大把数据出来了不利于查找, 于是就想起dos下可以同时指向多条命令,如是写下如下命令: netsta ...