verify/yosupo/string/suffix-automaton/number_of_substrings.test.cpp
Depends on
Code
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"
#include "template.hpp"
#include "string/suffix-automaton.hpp"
int main (){
string s ;
cin >> s ;
SuffixAutomaton sa ( s );
cout << sa . distinct_substrings ();
}
#line 1 "verify/yosupo/string/suffix-automaton/number_of_substrings.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"
#line 2 "template.hpp"
#include <bits/stdc++.h>
using namespace std ;
using ll = long long ;
using db = long double ;
using i128 = __int128_t ;
using u32 = uint32_t ;
using u64 = uint64_t ;
template < class T >
using PQ = priority_queue < T , vector < T > , greater < T >> ;
const int INF = INT_MAX / 2 ;
const ll LINF = LLONG_MAX / 4 ;
const db DINF = numeric_limits < db >:: infinity ();
const db EPS = 1e-9 ;
const db PI = acos ( db ( - 1 ));
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SZ(a) (int)(a.size())
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNI(a) a.erase(unique(ALL(a)),a.end())
#define LB(a,x) (int)(lower_bound(ALL(a),x)-a.begin())
#define UB(a,x) (int)(upper_bound(ALL(a),x)-a.begin())
#define FIND(a,x) (int)(find(ALL(a),x)-a.begin())
#define COUNT(a,x) (int)count(ALL(a),x)
#define MIN(a) *min_element(ALL(a))
#define MAX(a) *max_element(ALL(a))
template < class T , class U >
bool chmin ( T & a , U b ){ return b < a ? a = b , 1 : 0 ;}
template < class T , class U >
bool chmax ( T & a , U b ){ return a < b ? a = b , 1 : 0 ;}
template < class T >
T floor ( T a , T b ){ return a / b - (( a ^ b ) < 0 && a % b );}
template < class T >
T ceil ( T a , T b ){ return a / b + (( a ^ b ) >= 0 && a % b );}
int popcnt ( ll x ){ return __builtin_popcountll ( x );}
int topbit ( ll x ){ return x == 0 ?- 1 : 63 - __builtin_clzll ( x );}
int lowbit ( ll x ){ return x == 0 ?- 1 : __builtin_ctzll ( x );}
template < class T , class U >
T SUM ( const U & a ){ return accumulate ( ALL ( a ), T {});}
template < class T >
vector < int > argsort ( const vector < T > & a ){
vector < int > p ( SZ ( a ));
iota ( ALL ( p ), 0 );
stable_sort ( ALL ( p ),[ & ]( int i , int j ){ return a [ i ] < a [ j ];});
return p ;
}
template < class T , size_t D = 0 >
struct VecBuilder {
array < size_t , D > d ;
VecBuilder < T , D + 1 > operator []( size_t n ) const {
VecBuilder < T , D + 1 > res {};
for ( size_t i = 0 ; i < D ; i ++ ) res . d [ i ] = d [ i ];
res . d [ D ] = n ;
return res ;
}
template < size_t I = 0 >
auto build ( const T & x ) const {
if constexpr ( I + 1 == D ) return vector < T > ( d [ I ], x );
else return vector < decltype ( build < I + 1 > ( x )) > ( d [ I ], build < I + 1 > ( x ));
}
auto operator ()( const T & x = T ()) const {
static_assert ( D > 0 );
return build ( x );
}
};
template < class T >
inline constexpr VecBuilder < T > VEC {};
mt19937 rng ( chrono :: steady_clock :: now (). time_since_epoch (). count ());
mt19937_64 rng64 ( chrono :: steady_clock :: now (). time_since_epoch (). count ());
#line 2 "string/suffix-automaton.hpp"
/**
* Author: Teetat T.
* Date: 2024-06-14
* Description: Suffix Automaton.
*/
template < class STR >
struct SuffixAutomaton {
using T = typename STR :: value_type ;
struct Node {
map < T , int > nxt ;
int link , len ;
Node ( int link , int len ) : link ( link ), len ( len ){}
};
vector < Node > nodes ;
int last ;
SuffixAutomaton () : nodes { Node ( - 1 , 0 )}, last ( 0 ){}
SuffixAutomaton ( const STR & s ) : SuffixAutomaton (){
for ( auto c : s ) extend ( c );
}
int new_node ( int link , int len ){
nodes . emplace_back ( Node ( link , len ));
return ( int ) nodes . size () - 1 ;
}
void extend ( T c ){
int cur = new_node ( 0 , nodes [ last ]. len + 1 );
int p = last ;
while ( p !=- 1 &&! nodes [ p ]. nxt . count ( c )){
nodes [ p ]. nxt [ c ] = cur ;
p = nodes [ p ]. link ;
}
if ( p !=- 1 ){
int q = nodes [ p ]. nxt [ c ];
if ( nodes [ p ]. len + 1 == nodes [ q ]. len ){
nodes [ cur ]. link = q ;
} else {
int r = new_node ( nodes [ q ]. link , nodes [ p ]. len + 1 );
nodes [ r ]. nxt = nodes [ q ]. nxt ;
while ( p !=- 1 && nodes [ p ]. nxt [ c ] == q ){
nodes [ p ]. nxt [ c ] = r ;
p = nodes [ p ]. link ;
}
nodes [ q ]. link = nodes [ cur ]. link = r ;
}
}
last = cur ;
}
ll distinct_substrings (){
ll res = 0 ;
for ( int i = 1 ; i < ( int ) nodes . size (); i ++ ) res += nodes [ i ]. len - nodes [ nodes [ i ]. link ]. len ;
return res ;
}
};
#line 4 "verify/yosupo/string/suffix-automaton/number_of_substrings.test.cpp"
int main (){
string s ;
cin >> s ;
SuffixAutomaton sa ( s );
cout << sa . distinct_substrings ();
}
Back to top page