#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include //#include using namespace std; //using namespace atcoder; #define DEBUG #ifdef DEBUG template ostream &operator<<(ostream &os, const pair &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template ostream &operator<<(ostream &os, const vector &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector vl; typedef vector vvl; typedef vector vc; typedef vector vs; typedef vector vb; typedef vector vd; typedef pair P; typedef pair pii; typedef vector

vpl; typedef tuple tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = (1<<30)-1; const ll linf = 1LL<<61; const int MAX = 510000; int dy[8] = {0,1,0,-1,1,-1,-1,1}; int dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-7; template inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template inline bool chmax(T1 &a,T2 b){ if(a inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} const int mod = 1e9 + 7; //const int mod = 998244353; struct SA_IS{ std::vector sa; std::vector lcp; SA_IS(std::string str){ str += '$'; int n = str.size(); std::vector s(n); for(int i=0; i str, int k){ str.push_back(0); int n = str.size(); sa = sa_is(str, k); } void induced_sort(std::vector &sa, std::vector &s, std::vector &lms, std::string &ls, int k){ int n = s.size(); std::vector c_size(k,0), bin(k+1,0), count(k,0); for(int i=0; i=0; i--){ int id = s[lms[i]]; sa[bin[id+1] - 1 - count[id]] = lms[i]; count[id]++; } //L-sort std::fill(count.begin(), count.end(), 0); for(int i=0; i 0 && ls[sa[i]] == 'S' && ls[sa[i]-1] == 'L') sa[i] = -1; } //S-sort std::fill(count.begin(), count.end(), 0); for(int i=n-1; i>=0; i--){ if(sa[i] == -1) continue; int next = sa[i] - 1; if(next == -1 || ls[next] == 'L') continue; int id = s[next]; sa[bin[id+1] - 1 - count[id]] = next; count[id]++; } } std::vector sa_is(std::vector &s, int k){ int n = s.size(); std::vector sa(n,-1); std::vector is_lms(n,false); std::string ls; ls.resize(n); ls[n-1] = 'S'; for(int i=n-2; i>=0; i--){ if(s[i] < s[i+1]) ls[i] = 'S'; else if(s[i] > s[i+1]) ls[i] = 'L'; else ls[i] = ls[i+1]; } std::vector lms, sorted_lms; for(int i=0; i 0 && (is_lms[x+d] || is_lms[y+d])){ if(is_lms[x+d] != is_lms[y+d]) same = false; break; } if(s[x+d] != s[y+d]){ same = false; break; } d++; } if(!same) kind++; sa[sorted_lms[i+1]] = kind; } std::vector new_str; for(int i=0; i new_lms(lms.size()); if(kind + 1 == lms.size()){ new_lms = sorted_lms; }else{ sorted_lms = sa_is(new_str, kind + 1); for(int i=0; i ran(n+1); for(int i=0; i<=n; i++) ran[sa[i]] = i; int h = 0; lcp[0] = 0; for(int i=0; i 0) h--; for(; j+h> s; int n = s.size(); int q; cin >> q; SA_IS sa(s); ll ans = 0; rep(i,q){ string t; cin >> t; int l = 0, r = n+1; int m = t.size(); while(r-l > 1){ int mi = (r+l)/2; int id = sa.sa[mi]; if(s.substr(id,min(m,n-id)) < t) l = mi; else r = mi; } int L = l; l = 0, r = n+1; while(r-l > 1){ int mi = (r+l)/2; int id = sa.sa[mi]; if(s.substr(id,min(m,n-id)) <= t) l = mi; else r = mi; } ans += l-L; } cout << ans << endl; }