Submit Info #3667

Problem Lang User Status Time Memory
Suffix Array cpp (anonymous) AC 78 ms 15.71 MiB

ケース詳細
Name Status Time Memory
all_same_00 AC 47 ms 9.71 MiB
all_same_01 AC 48 ms 9.71 MiB
all_same_02 AC 52 ms 9.71 MiB
all_same_03 AC 52 ms 9.75 MiB
all_same_04 AC 50 ms 9.71 MiB
example_00 AC 1 ms 0.67 MiB
example_01 AC 0 ms 0.71 MiB
example_02 AC 0 ms 0.67 MiB
example_03 AC 1 ms 0.67 MiB
fib_str_00 AC 63 ms 15.04 MiB
fib_str_01 AC 43 ms 11.08 MiB
fib_str_02 AC 49 ms 12.22 MiB
fib_str_03 AC 37 ms 8.88 MiB
fib_str_04 AC 63 ms 15.71 MiB
max_random_00 AC 75 ms 14.46 MiB
max_random_01 AC 75 ms 14.58 MiB
max_random_02 AC 78 ms 14.45 MiB
max_random_03 AC 76 ms 14.45 MiB
max_random_04 AC 74 ms 14.46 MiB
random_00 AC 59 ms 11.66 MiB
random_01 AC 72 ms 13.71 MiB
random_02 AC 9 ms 2.29 MiB
random_03 AC 69 ms 12.77 MiB
random_04 AC 44 ms 8.54 MiB

#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define lfs #define test cout<<"test"<<endl; #define fi first #define se second #define MP make_pair #define PB push_back #define EB emplace_back #define rep(i,n,m) for(ll i = n; i < (ll)(m); i++) #define rrep(i,n,m) for(ll i = m - 1; i >= (ll)(n); i--) using ll = int; using ld = long double; const ll MOD = 1e9+7; //const ll MOD = 998244353; //const ll INF = 1e18; using P = pair<ll, ll>; template<typename T> void chmin(T &a,T b){if(a>b)a=b;} template<typename T> void chmax(T &a,T b){if(a<b)a=b;} void pmod(ll &a,ll b){a=(a+b)%MOD;} void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;} void qmod(ll &a,ll b){a=(a*b)%MOD;} void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;} ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});} void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;} void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;} void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;} template<typename T1,typename T2> void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++) {cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}}; void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++) {for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}}; template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0]; for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;}; template<typename T> vector<vector<T>>vec(ll x, ll y, T w){ vector<vector<T>>v(x,vector<T>(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vector<ll>dx={1,0,-1,0,1,1,-1,-1}; vector<ll>dy={0,1,0,-1,1,-1,1,-1}; template<typename T> vector<T> make_v(size_t a,T b){return vector<T>(a,b);} template<typename... Ts> auto make_v(size_t a,Ts... ts){ return vector<decltype(make_v(ts...))>(a,make_v(ts...)); } struct SuffixArray{ ll size; string s; vector<ll>data; SuffixArray(string S):size(S.size()),s(S){ s += '$'; vector<ll>input(s.size()); for(ll i=0;i<s.size();i++)input[i] = s[i]; data = induced_sort(input, 200); data.erase(data.begin()); } vector<ll> induced_sort(vector<ll>t, ll kind){ ll sz = t.size(); vector<bool>ls(sz);//trueはL型,falseはS型 for(ll i = sz-1;i>=0;i--){ if(i==sz-1)ls[i] = false; else{ if(t[i]!=t[i+1])ls[i] = (t[i] > t[i+1]); else ls[i] = ls[i+1]; } } vector<ll>cnt(kind); for(ll i=0;i<sz;i++)cnt[t[i]]++; vector<P>lr(kind,MP(-1,-1)); for(ll i=1;i<kind;i++){ if(cnt[i]==0)lr[i] = lr[i-1]; else lr[i] = make_pair(lr[i-1].se + 1, lr[i-1].se + cnt[i]); } vector<ll>lmsidx,ret(sz,-1); for(ll i=0;i<sz-1;i++){ if(ls[i]&&!ls[i+1]){ ret[lr[t[i+1]].fi+cnt[t[i+1]]-1]=i+1; cnt[t[i+1]]--; lmsidx.push_back(i+1); } } fill(ALL(cnt),0); for(ll i=0;i<sz;i++){ if(ret[i]>=1&&ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].fi+cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; if(i!=0&&!ls[ret[i]])ret[i]=-1; } } fill(ALL(cnt),0); for(ll i=sz-1;i>=1;i--){ if(ret[i]>=1&&!ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].se-cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; } } vector<ll>rev_lmsidx(sz,-1),lmsinput(lmsidx.size(),-1); for(ll i=0;i<lmsidx.size();i++)rev_lmsidx[lmsidx[i]] = i; ll kindnum=1; lmsinput[rev_lmsidx[ret[0]]] = 1; vector<ll>comp(t.begin()+ret[0],t.end()); for(ll i=1;i<sz;i++){ if(ret[i]>=1&&ls[ret[i]-1]&&!ls[ret[i]]){ vector<ll>tmp(t.begin()+ret[i],t.begin()+lmsidx[rev_lmsidx[ret[i]]+1]+1); if(comp != tmp){ kindnum++; comp = tmp; } lmsinput[rev_lmsidx[ret[i]]] = kindnum; } } vector<ll>output; if(kindnum==lmsidx.size()){ output.assign(kindnum,-1); for(ll i=0;i<lmsinput.size();i++)output[lmsinput[i]-1]=i; } else output = induced_sort(lmsinput,kindnum+1); fill(ALL(cnt),0), fill(ALL(ret),-1); for(ll i=output.size()-1;i>=0;i--){ ll tmp=lmsidx[output[i]]; ret[lr[t[tmp]].se - cnt[t[tmp]]]=tmp; cnt[t[tmp]]++; } fill(ALL(cnt),0); for(ll i=0;i<sz;i++){ if(ret[i]>=1&&ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].fi+cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; if(i!=0&&!ls[ret[i]])ret[i]=-1; } } fill(ALL(cnt),0); for(ll i=sz-1;i>=1;i--){ if(ret[i]>=1&&!ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].se-cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; } } return ret; } }; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); //cout<<fixed<<setprecision(10); ll res=0,buf=0; bool judge = true; string s;cin>>s; SuffixArray sa(s); debug(sa.data,s.size()); /*rep(i,0,s.size()){ cout<<sa.data[i] spa s.substr(sa.data[i])<<endl; }*/ return 0; }