博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[USACO 2011 Dec Gold] Threatening Letter【后缀】
阅读量:4560 次
发布时间:2019-06-08

本文共 3311 字,大约阅读时间需要 11 分钟。

Problem 3: Threatening Letter [J. Kuipers, 2002]FJ has had a terrible fight with his neighbor and wants to send hima nasty letter, but wants to remain anonymous. As so many beforehim have done, he plans to cut out printed letters and paste themonto a sheet of paper. He has an infinite number of the most recentissue of the Moo York Times that has N (1 <= N <= 50,000) uppercaseletters laid out in a long string (though read in as a series ofshorter strings). Likewise, he has a message he'd like to composethat is a single long string of letters but that is read in as aset 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 removeany single-line snippet from the Moo York Times with one cut. Henotices 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 hisletter 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        GJUMPSOVERTHELAZYDOGfrom which FJ wants to construct a 9 letter message:        FOXDOG        DOGThese input lines represent a pair of strings:        THEQUICKBROWNFOXDOGJUMPSOVERTHELAZYDOG        FOXDOGDOGSince "FOXDOG" exists in the newspaper, FJ can cut this piece outand then get the last "DOG" by cutting out either instance of theword "DOG".Thus, he requires but two cuts.PROBLEM NAME: letterINPUT 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 9THEQUICKBROWNFOXDOGJUMPSOVERTHELAZYDOGFOXDOGDOGOUTPUT FORMAT:* Line 1: The minimum number of cuts FJ has to make to create his        messageSAMPLE OUTPUT (file letter.out):2

 

 

一看跟子串相关,就是后缀那一套了。想法是这样的,尽量在文本串中找到更长的子串与当前串匹配。若无法继续匹配了,则重新匹配,答案+1。这里我选择了后缀自动机,实现起来好写。

#include 
#include
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的模版了叭。

转载于:https://www.cnblogs.com/ciao-sora/p/6040791.html

你可能感兴趣的文章
Ubuntu创建新用户并增加管理员权限
查看>>
MySQL库目录下db.opt文件的作用
查看>>
HTTP_referrer
查看>>
模拟表单方式上传文件
查看>>
消息中间件--ActiveMQ&JMS消息服务
查看>>
VS调SQL中存储过程实现登陆
查看>>
ubuntu安装hadoop2.6
查看>>
技术栈
查看>>
Docker Centos安装Redis以及问题处理
查看>>
解决JS弹出新窗口被浏览器阻止的解决方案
查看>>
最短路——SPFA算法模板
查看>>
ASP.NET Web Game 构架设计2--数据库设计
查看>>
第六章.解决大问题
查看>>
ASIHTTP
查看>>
iOS 9: UIStackView入门
查看>>
Android——SQLite数据库(一)创建数据库、创建表、初始化数据
查看>>
自然语言处理Attention Model
查看>>
URAL 1152. False Mirrors (记忆化搜索 状压DP)
查看>>
操作mdb文件的帮助类
查看>>
mysql优化
查看>>