@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. Delphi代码规范

    1. 前言 本文档主要是为Delphi开发人员提供一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致格式可遵循.这样,每个编程人员编写的代码能够被其他人理解. 2. 源程序书写规范 2 ...

  2. 观察者模式(Observer、Subject、ConcreteSubject、ConcreteObserver)(监护、订阅)

    建立一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应的作出反应. 在此发生改变的对象称之为观察目标(被观察者),而被通知的对象称为观察者,一个观察者目标可以对应多个观 ...

  3. 关于SQL查询效率 主要针对sql server

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享:机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试,比 ...

  4. HDFS 名称节点的启动

  5. Hadoop IO 特性详解(1)【数据完整性】

    本文结合hadoop : the definitive guide精心而作,包含作者的心血,希望可以帮助大家理解一点hdfs的皮毛,足矣.(charles@xingbod.cn) hadoop本身自带 ...

  6. TZ_06_SpringMVC_常用注解

    1. @Controller@RequestMapping(path = "/user")//一级目录 public class FormSubmit { @RequestMapp ...

  7. 完美解决IE8不支持margin auto问题

    不用js,超级简单,完美支持. body下的整个container .container { overflow: hidden; margin: 0px auto; text-align: cente ...

  8. jnhs-SpringMVC的controller向jsp传递数据的五种方式

    参考此文http://blog.sina.com.cn/s/blog_6d3c1ec601014h1h.html 1 使用ModelAndVoew 引入:org.springframework.web ...

  9. python应用文件读取写登录注册

    #!/usr/bin/python3# -*- coding: utf-8 -*-# Author: zhw#读取文件中的内容def open_file(filename ,file_type , * ...

  10. NKOJ3485 【2015多校联训4】数据

    问题描述 Mr_H 出了一道信息学竞赛题,就是给 n 个数排序.输入格式是这样的:试题有若干组数据.每组数据的第一个是一个整数 n,表示总共有 n 个数待排序:接下来 n 个整数,分别表示这n 个待排 ...