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;
    }
    
  • 相关阅读:
    博客园设置简约主题
    day25-48
    day1-10
    drf app
    flask 框架 练习
    vue入门 显示数据 操作属性 操作样式 条件渲染
    flask 配置文件 路由 视图 模板 中间件
    flask 使用数据库连接池
    Android 工程中添加依赖
    apk反编译
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6414068.html
Copyright © 2011-2022 走看看