bzoj 2251: 外星联络 后缀Trie

题目大意

http://www.lydsy.com/JudgeOnline/problem.php?id=2251

题解

本来以为这道题应该从01序列的性质入手
结果就想歪了
等自己跳出了01序列这个思维
就马上看到了一颗Trie树。。。

这道题的较难思考的地方在于无法确定出每个字符串的字典序
所以我们想到了和字典序有关的东西sort+string字典树
我们知道所有后缀的所有前缀一定能取到所有字串
字典树的dfs序列得到的dfs序是按照字典序排列的
所以我们可以理所当然地用字典树来搞这个东西

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 3010;
int ch[maxn*maxn][2];
int num[maxn*maxn],nodecnt,n;
char s[maxn];
inline void insert(int i){
	int nw = 0;
	for(;i<n;++i){
		if(ch[nw][s[i]-'0'] == 0) ch[nw][s[i]-'0'] = ++nodecnt;
		nw = ch[nw][s[i] - '0'];
		++num[nw];
	}
}
inline void dfs(int u){
	if(num[u] > 1) printf("%d\n",num[u]);
	if(ch[u][0]) dfs(ch[u][0]);
	if(ch[u][1]) dfs(ch[u][1]);
}
int main(){
	read(n);scanf("%s",s);
	for(int i=0;i<n;++i) insert(i);
	dfs(0);
	getchar();getchar();
	return 0;
}
posted @ 2017-02-18 20:53  Sky_miner  阅读(130)  评论(0编辑  收藏  举报