【USACO】2017 December Contest, Platinum题解

【比赛经历】

  • 大概顺利满分了,就是T2的代码比较难调。
  • T2能够直观地反映出GDB和输出调试结合的优越性。

【T1】Standing Out from the Herd

【题目链接】

【题解链接】

【思路要点】

  • 后缀的前缀是子串,考虑使用后缀结构来解题。笔者选用的是后缀树。
  • 对所有询问串建立多串的后缀树,DFS,在访问一个子树中后缀标记唯一的点时,将其父边的长度加到对应字符串的答案计数器中即可。
  • 时间复杂度\(O(\sum|S|)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	200005
#define MAXC	26
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct Suffix_Automaton {
	int child[MAXN][MAXC];
	int father[MAXN], depth[MAXN];
	int root, size, last, n;
	vector <int> a[MAXN];
	int colour[MAXN];
	bool unq[MAXN];
	long long ans[MAXN];
	int new_node(int dep) {
		depth[size] = dep;
		unq[size] = true;
		return size++;
	}
	void init() {
		size = 0;
		root = last = new_node(0);
	}
	void addcol(int pos, int col) {
		if (!unq[pos]) return;
		if (colour[pos] == 0) colour[pos] = col;
		else if (col != colour[pos]) unq[pos] = false;
	}
	void Extend(int ch, int col) {
		int np = child[last][ch];
		if (np) {
			if (depth[np] == depth[last] + 1)  {
				addcol(np, col);
				last = np;
			} else {
				int nq = new_node(depth[last] + 1);
				father[nq] = father[np];
				father[np] = nq;
				memcpy(child[nq], child[np], sizeof(child[np]));
				for (int p = last; child[p][ch] == np; p = father[p])
					child[p][ch] = nq;
				addcol(nq, col)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值