@description@

给定一个大小为 G 的字符集,并给定一个长度为 N 的字符串 A。

求最短不是 A 的子序列的字符串的长度为 L,以及长度为 L 的不是 A 的子序列的字符串数量 X。

1 <= N <= 2,000,000; 1 <= G <= 10^9。

原题戳这里

@solution@

暴力做法?考虑将 A 的所有子序列塞进 trie 里面,则 trie 中出度不为 G 的点后面加字符一定不是 A 的子序列。

注意到这是一个有限状态自动机,可以识别 A 的所有子序列(oi-wiki 上好像把它叫作序列自动机?)。

考虑简化这个自动机。记结点 i 能识别的字符串为以 A[i] 为结尾的,且在 A[1...i-1] 中没有出现的子序列。

假设 i 前面第一个与它相同的位置在 lst[i],则 lst[i] 之前的连向 i 就会重复,而 [lst[i], i-1] 连向 i 恰好是所有可能的子序列。

注意到这是一个区间的连边形式,我们可以线段树优化连边。

(还有另一种理解方式:第 i 个点的字符 j 转移边,连向 i 之后第一个 j 的出现位置。这个解释可以在 oi-wiki 中看到。)

注意一个结点可以表示多个字符串,而有用的只有这个结点能够表示的最短的字符串。

我们处理出 len[i] 表示以 i 结尾的最短子序列长度,并记 cnt[i] 表示最短子序列的种类数。

连完边后一样看出度是否为 G。

记 oud[i] 表示 i 的出度,则 i 对 len[i] + 1 长度有着 (G - oud[i])*cnt[i] 的贡献。

实际操作并不需要真的连边,只需要用线段树维护上面三种信息。

@accepted code@

#include <unordered_map>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class ShortestMissingSubsequences{
private :
#define MAXN 2000000
#define MOD 1000000007
typedef pair<int, int> pii;
typedef long long ll; unordered_map<int, int>mp;
int A[MAXN + 5], lst[MAXN + 5];
int len[MAXN + 5], tot[MAXN + 5], cnt[MAXN + 5]; #define lch (x<<1)
#define rch (x<<1|1)
pii tag[4*MAXN + 5]; int nw[4*MAXN + 5];
pii merge(pii a, pii b) {
pii c; c.first = min(a.first, b.first);
if( c.first == a.first ) c.second = (c.second + a.second) % MOD;
if( c.first == b.first ) c.second = (c.second + b.second) % MOD;
return c;
}
void pushup(int x) {tag[x] = merge(tag[lch], tag[rch]);}
pii get(int x, int l, int r, int ql, int qr) {
if( ql <= l && r <= qr ) {
nw[x]++;
return tag[x];
}
if( ql > r || qr < l )
return make_pair(MAXN + 5, 0);
int m = (l + r) >> 1;
return merge(get(lch, l, m, ql, qr), get(rch, m + 1, r, ql, qr));
}
void update(int x, int l, int r, int p) {
if( l == r ) {
tag[x] = make_pair(len[p], tot[p]);
return ;
}
int m = (l + r) >> 1;
if( p <= m ) update(lch, l, m, p);
else update(rch, m + 1, r, p);
pushup(x);
}
void push(int x, int l, int r, int d) {
d += nw[x];
if( l == r ) {
cnt[l] = d;
return ;
}
int m = (l + r) >> 1;
push(lch, l, m, d), push(rch, m + 1, r, d);
}
public :
vector<int>count(int G, int N, vector<int>Aprefix) {
int M = Aprefix.size();
for(int i=0;i<M;i++) A[i+1] = Aprefix[i];
ll state = A[M];
for(int i=M+1;i<=N;i++) {
state = (state * 1103515245LL + 12345) % (1LL<<31);
A[i] = state % G;
}
for(int i=1;i<=N;i++)
lst[i] = mp[A[i]], mp[A[i]] = i;
len[0] = 0, tot[0] = 1, update(1, 0, N, 0);
for(int i=1;i<=N;i++) {
pii p = get(1, 0, N, lst[i], i - 1);
len[i] = p.first + 1, tot[i] = p.second;
update(1, 0, N, i);
}
push(1, 0, N, 0);
int mn = MAXN + 5;
for(int i=0;i<=N;i++)
if( cnt[i] != G )
mn = min(mn, len[i]);
vector<int>ans; ans.push_back(mn + 1);
int x = 0;
for(int i=0;i<=N;i++)
if( len[i] == mn )
x = (x + 1LL*tot[i]*(G - cnt[i])%MOD) % MOD;
ans.push_back(x);
return ans;
}
};

@details@

被小小地卡了一下常。。。

@topcoder - SRM766R1 D1L3@ ShortestMissingSubsequences的更多相关文章

  1. Topcoder SRM570 D1L3 CurvyonRails

    几个样例: 5 5wCCwwwCC....w......www..wReturns: 0 3 3C.w....C.Returns: 1 21 20CC..CCCw.CwC..CC.w.CC.CCCwC ...

  2. @topcoder - 2013TCO3A D1L3@ TrickyInequality

    目录 @description@ @accepted code@ @accepted code@ @details@ @description@ 现有不等式组: \[\begin{cases} x_1 ...

  3. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  4. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  5. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  6. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  7. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  8. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  9. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

随机推荐

  1. Spring Cloud Eureka集群配置及注意事项(Greenwich版本)

    Spring Cloud Eureka集群配置及注意事项(Greenwich版本) 一·概述 Spring Cloud Netflix Eureka 是一个提供服务注册与发现的套件.服务提供者只需要将 ...

  2. KOA 学习(四)

    响应(Response) Koa Response 对象是对 node 的 response 进一步抽象和封装,提供了日常 HTTP 服务器开发中一些有用的功能. response.header Re ...

  3. 生成pb模型出错

    raise self.ParseError('Expected identifier or number, got %s.' % result)google.protobuf.text_format. ...

  4. textarea标签中多出的空格

    //这么写才能被正确渲染 <textarea></textarea> //这样就会有空格 <textarea> </textarea> 不能换行,涨姿势

  5. hdu 2586 (lca-RMQ)

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  6. C# 基础才是重中之重~对象的生与死

    为何要写 之所以写这篇文章,完全是因为学生们在实际开发中遇到的问题,一个对象占用的内存空间总不被释放,导致系统内存不断攀升,其最主要原因是我们对“对象的生与死”不清楚,或者从来没有认真去考虑过这件事, ...

  7. applyMiddleware源码中的闭包

    闭包都是个老掉牙的话题了,这次又提起,是因为重看Redux源码时发现了applyMiddleware里的用法很巧妙.我们先看一个简单的例子. var a = (num) => num + 1 v ...

  8. IbatchBolt和BaseTransactionalBolt区别

    void prepare(java.util.Map conf, TopologyContext context, BatchOutputCollector collector, T id) T id ...

  9. 区间加值,区间gcd, 牛客949H

    牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...

  10. python中星号

    第一种情况:用在两表达式的中间,*表示乘法,**表示取幂,如: 1 2 3 4 >>> 2*5 10 >>> 2**7 128 第二种情况:用在变量的前面. 1,向 ...