zoukankan      html  css  js  c++  java
  • hihocoder #1445 : 后缀自动机二·重复旋律5

    统计本质不同的字串板子题

    把每个状态里的串统计一下就行了

     链接 :https://hihocoder.com/problemset/problem/1445

    #include<bits/stdc++.h>
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,a,n) for(int i=n;i>=a;--i)
    #define pb push_back
    #define fi first
    #define se second
    #define io std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, INF = 0x3f3f3f3f;
    
    ll gcd(ll a,ll b)
    {
        return b?gcd(b,a%b):a;
    }
    ll qpow(ll a,ll n)
    {
        ll r=1%P;
        for (a%=P; n; a=a*a%P,n>>=1)if(n&1)r=r*a%P;
        return r;
    }
    const int maxn=2e6;
    
    struct Suffix_Automata {
      int maxlen[maxn], trans[maxn][26], link[maxn], Size, Last;
      int siz[maxn];
      Suffix_Automata() { Size = Last = 1; }
      inline void Extend(int id) {
        int cur = (++ Size), p;
        siz[Size]=1;
        maxlen[cur] = maxlen[Last] + 1;
        for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
        if (!p) link[cur] = 1;
        else {
          int q = trans[p][id];
          if (maxlen[q] == maxlen[p] + 1) link[cur] = q;
          else {
            int clone = (++ Size);
            maxlen[clone] = maxlen[p] + 1;
            memcpy(trans[clone], trans[q],sizeof(trans[q]));
            link[clone] = link[q];
            for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
            link[cur] = link[q] = clone;
          }
        } 
        Last = cur;
      }
    } T;
    int t[maxn],A[maxn];
    
    int main()
    {
         char s[maxn];
         cin>>s+1;
         int n=strlen(s+1);
         for(int i=1;i<=n;i++)
         {
           T.Extend(s[i]-'a');
         }
         ll ans=0;
         for(int i=1;i<=T.Size;i++)
         {
           ans+=T.maxlen[i]-T.maxlen[T.link[i]];
         }
         cout<<ans<<endl;
    
    }
  • 相关阅读:
    stack.pop()和stack.peek()的区别
    信号与系统,系统函数的影响
    java中short、int、long、float、double取值范围
    Spring从容器获得组件的方法
    Eclipse中项目的类路径文件夹
    Math的常用方法
    spring基本入门步骤
    opencv入门
    make和cmake构建工具
    使用eclipse开发c++程序及开发环境搭建
  • 原文地址:https://www.cnblogs.com/acmLLF/p/13496588.html
Copyright © 2011-2022 走看看