zoukankan      html  css  js  c++  java
  • SPOJ 8222 Substrings 后缀自动机

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.

    Input

    String S consists of at most 250000 lowercase latin letters.

    Output

    Output |S| lines. On the i-th line output F(i).

    Example

    Input:
    ababa

    Output:
    3
    2
    2
    1
    1

    没什么可说的
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define maxn 500005
     8 using namespace std;
     9 char s[maxn];
    10 int len;
    11 struct data {
    12     int son[maxn][26],link[maxn],step[maxn],last,cnt,size[maxn];
    13     int v[maxn],pos[maxn],f[maxn];
    14     data() {last=cnt=1;}
    15     void extend(int c) {
    16         int p=last,np=last=++cnt;step[np]=step[p]+1;size[np]=1;
    17         while(p&&!son[p][c]) son[p][c]=np,p=link[p];
    18         if(!p) link[np]=1;
    19         else  {
    20             int q=son[p][c];
    21             if(step[q]==step[p]+1) link[np]=q;
    22             else {
    23                 int nq=++cnt;
    24                 memcpy(son[nq],son[q],sizeof(son[q]));
    25                 link[nq]=link[q];
    26                 link[q]=link[np]=nq;
    27                 step[nq]=step[p]+1;
    28                 while(p&&son[p][c]==q) son[p][c]=nq,p=link[p];
    29             }
    30         }
    31     }
    32     void pre() {
    33         for(int i=1;i<=cnt;i++) v[step[i]]++;
    34         for(int i=1;i<=cnt;i++) v[i]+=v[i-1];
    35         for(int i=cnt;i;i--) pos[v[step[i]]--]=i;
    36         for(int i=cnt;i;i--) size[link[pos[i]]]+=size[pos[i]];
    37         for(int i=1;i<=cnt;i++) f[step[i]]=max(f[step[i]],size[i]);
    38         for(int i=len;i;i--) f[i]=max(f[i],f[i+1]);
    39         for(int i=1;i<=len;i++) printf("%d
    ",f[i]);
    40     }
    41 }sam;
    42 int main() {
    43     scanf("%s",s+1);
    44     len=strlen(s+1);
    45     for(int i=1;i<=len;i++) sam.extend(s[i]-'a');
    46     sam.pre();
    47     return 0;
    48 }
    View Code
    O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
  • 相关阅读:
    剑桥雅思写作高分范文ESSAY81
    maven安装配置
    IntelliJ IDEA 2017.3.1安装步骤
    Git基本命令整理
    jacoco覆盖率工具测试及性能分析
    OSGI框架
    查看指定库对应GCC版本
    普元eos、soa、esb
    emp架构
    jar包安装到本地仓库
  • 原文地址:https://www.cnblogs.com/wls001/p/8270021.html
Copyright © 2011-2022 走看看