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;
}

转载于:https://www.cnblogs.com/Skyminer/p/6414068.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值