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. cors跨域深刻理解

    1.跨域问题只出现在前端和后端不在同一个主机上.前后端在同一个主机上不会出现跨域问题. 2.浏览器的一种自我保护机制,不允许出现本地浏览器ajax异步请求访问127.0.0.1以外的系统,因为浏览器不 ...

  2. 【c++】C++中const用法总结

    1.      const常量,如const int max = 100; 优点:const常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类型安全 ...

  3. 系统重装 Ghost系统的disk to image等等是什么意思

    localdiskto disk to imade from imagepartitionto partition to image from imagecheckimage file disk这些是 ...

  4. Redis 入门指南

    就是DBIdx

  5. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  6. 使用Blender批量导出/转换模型

    2.4版本号的Blender API和2.5以上版本号的API有非常大的不同,这里仅仅是提供了思路和2.4版本号的导出方案. 先提供一个脚本,这个是由Blender调用的.用于转换Ogre的Mesh文 ...

  7. jquery源码学习笔记一:总体结构

    练武不练功,到老一场空.计算机也一样. 计算机的功,就是原理.如果程序员只会使用各种函数,各种框架,而不知其原理,顶多熟练工人而已.知其然,更要知其所以然. jquery我们用得很爽,但它究竟咋实现的 ...

  8. iOS开发——高级篇——iOS开发之网络安全密码学

    一.非对称加密 - RSA : + 公钥加密,私钥解密: + 私钥加密,公钥解密: + 只能通过因式分解来破解 二.对称加密 - DES - 3DES - AES (高级密码标准,美国国家安全局使用, ...

  9. Ant 打包 问题

    Ant 打包问题及解决归纳总结: 1.build.xml注意设置basedir: <project name="s2si"  default="dist" ...

  10. Entity Framework底层操作封装V2版本号(3)

    如今是附加的,组合查询须要的扩展类.大家知道lanmda表达式的组合条件比較麻烦,所以就加了一样一个类,方便进行组合查询: using System; using System.Collections ...