library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub tko919/library

:heavy_check_mark: Verify/LC_number_of_substrings.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"

#include "Template/template.hpp"

#include "String/suffixarray.hpp"


int main(){
    string s;
    cin>>s;
    int n=s.size();
    SuffixArray sa(s);
    ll ret=n-sa.sa[0];
    rep(i,1,n+1)ret+=n-sa.sa[i]-sa.lcp[i-1];
    cout<<ret<<'\n';
    return 0;
}
#line 1 "Verify/LC_number_of_substrings.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"

#line 1 "Template/template.hpp"
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b-1); i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T, template <class> class C>
ostream &operator<<(ostream &os, const C<T> &v) {
    os << "[";
    for (auto d : v)
        os << d << ", ";
    os << "]";
    return os;
}
#line 2 "String/suffixarray.hpp"

template<typename T>struct SuffixArray{
    T base;
    vector<int> sa,rsa,lcp;
    SuffixArray(const T& _s):base(_s){
        int n=base.size();
        auto p=minmax_element(ALL(base));
        int k=*p.second-*p.first+1;
        vector<int> t(n);
        rep(i,0,n)t[i]=base[i]-*p.first;
        sais(t,k);
        rsa.assign(n+1,-1);
        rep(i,0,n+1)rsa[sa[i]]=i;
        build(t);
    }
    void sais(vector<int> s,int k){
        int n=s.size();
        for(int& c:s)c++;
        s.push_back(0);
        k++;
        vector<bool> iss(n+1);
        vector<int> lms,bin(k+1,0);
        iss[n]=1;
        bin[1]=1;
        for(int i=n-1;i>=0;i--){
            iss[i]=(s[i]!=s[i+1]?s[i]<s[i+1]:iss[i+1]);
            if(!iss[i]&&iss[i+1])lms.push_back(i+1);
            bin[s[i]+1]++;
        }
        rep(i,0,k)bin[i+1]+=bin[i];
        auto induced=[&](const vector<int>& _lms)->void{
            sa.assign(n+1,-1);
            vector<int> cnt(k,0);
            for(int x:_lms){
                sa[bin[s[x]+1]-cnt[s[x]]-1]=x;
                cnt[s[x]]++;
            }
            cnt.assign(k,0);
            rep(i,0,n+1){
                int x=sa[i]-1;
                if(x>=0&&!iss[x]){
                    sa[bin[s[x]]+cnt[s[x]]]=x;
                    cnt[s[x]]++;
                }
            }
            cnt.assign(k,0);
            for(int i=n;i>=0;i--){
                int x=sa[i]-1;
                if(x>=0&&iss[x]){
                    sa[bin[s[x]+1]-cnt[s[x]]-1]=x;
                    cnt[s[x]]++;
                }
            }
        };
        induced(lms);
        if(lms.size()<=1)return;
        int m=lms.size();
        vector<int> rev(n+1,-1);
        rep(i,0,m)rev[lms[i]]=i;
        int idx=0,rec_k=1;
        vector<int> lmss(m),rec_s(m);
        for(int x:sa)if(rev[x]!=-1)lmss[idx++]=x;
        rec_s[m-1-rev[lmss[1]]]=1;
        rep(i,2,m){
            int xl=lmss[i],yl=lmss[i-1];
            int xr=lms[rev[xl]-1],yr=lms[rev[yl]-1];
            if(xr-xl!=yr-yl)rec_k++;
            else while(xl<=xr){
                if(s[xl]!=s[yl]){
                    rec_k++;
                    break;
                }
                xl++,yl++;
            }
            rec_s[m-1-rev[lmss[i]]]=rec_k;
        }
        sais(rec_s,rec_k+1);
        idx=m;
        rep(i,1,m+1)lmss[--idx]=lms[m-1-sa[i]];
        induced(lmss);
    }
    void build(const vector<int>& s){
        int n=s.size(),k=0;
        lcp.resize(n);
        rep(i,0,n+1){
            if(rsa[i]){
                for(int j=sa[rsa[i]-1];max(i,j)+k<n;k++){
                    if(s[i+k]!=s[j+k])break;
                }
                lcp[rsa[i]-1]=k;
            }
            if(k)k--;
        }
    }
    array<int,2> search(const T& t){
        int n=sa.size(),m=t.size();
        array<int,2> ret;
        int L=0,R=n;
        while(R-L>1){
            int mid=(L+R)>>1;
            if(base.compare(sa[mid],m,t)<0)L=mid;
            else R=mid;
        }
        ret[0]=R;
        L=0,R=n;
        while(R-L>1){
            int mid=(L+R)>>1;
            if(base.compare(sa[mid],m,t)<=0)L=mid;
            else R=mid;
        }
        ret[1]=R;
        return ret;
    }
};

/**
 * @brief Suffix Array
 * @docs docs/suffixarray.md
 */
#line 5 "Verify/LC_number_of_substrings.test.cpp"

int main(){
    string s;
    cin>>s;
    int n=s.size();
    SuffixArray sa(s);
    ll ret=n-sa.sa[0];
    rep(i,1,n+1)ret+=n-sa.sa[i]-sa.lcp[i-1];
    cout<<ret<<'\n';
    return 0;
}
Back to top page