zoukankan      html  css  js  c++  java
  • 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
    ",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;
    }
    
  • 相关阅读:
    0429 Scrum团队成立与第6-7章读后感
    0428 团队2.0
    0422 寻找数学口袋精灵BUG
    0422 Step2-FCFS调度
    0415 博客评价
    0414 结对--软件再升级(韩麒麟 列志华)
    0408 结对做汉堡
    0406 复利计算器--结对 组员 韩麒麟 列志华
    0405 构建之法第4章 读后感
    文法分析
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6414068.html
Copyright © 2011-2022 走看看