結果
問題 | No.1018 suffixsuffixsuffix |
ユーザー | n_vip |
提出日時 | 2020-04-03 23:04:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 282 ms / 2,000 ms |
コード長 | 5,372 bytes |
コンパイル時間 | 2,189 ms |
コンパイル使用メモリ | 155,824 KB |
実行使用メモリ | 54,964 KB |
最終ジャッジ日時 | 2024-07-03 05:38:52 |
合計ジャッジ時間 | 8,882 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 80 ms
26,684 KB |
testcase_10 | AC | 81 ms
26,916 KB |
testcase_11 | AC | 80 ms
27,060 KB |
testcase_12 | AC | 70 ms
26,848 KB |
testcase_13 | AC | 72 ms
26,880 KB |
testcase_14 | AC | 256 ms
54,584 KB |
testcase_15 | AC | 260 ms
53,692 KB |
testcase_16 | AC | 279 ms
54,672 KB |
testcase_17 | AC | 255 ms
54,088 KB |
testcase_18 | AC | 252 ms
54,196 KB |
testcase_19 | AC | 20 ms
6,940 KB |
testcase_20 | AC | 21 ms
6,940 KB |
testcase_21 | AC | 21 ms
6,940 KB |
testcase_22 | AC | 21 ms
6,944 KB |
testcase_23 | AC | 22 ms
6,944 KB |
testcase_24 | AC | 257 ms
54,464 KB |
testcase_25 | AC | 277 ms
54,964 KB |
testcase_26 | AC | 255 ms
53,640 KB |
testcase_27 | AC | 255 ms
53,636 KB |
testcase_28 | AC | 255 ms
53,772 KB |
testcase_29 | AC | 27 ms
6,940 KB |
testcase_30 | AC | 26 ms
6,944 KB |
testcase_31 | AC | 26 ms
6,940 KB |
testcase_32 | AC | 26 ms
6,944 KB |
testcase_33 | AC | 25 ms
6,940 KB |
testcase_34 | AC | 2 ms
6,940 KB |
testcase_35 | AC | 21 ms
6,944 KB |
testcase_36 | AC | 25 ms
6,940 KB |
testcase_37 | AC | 282 ms
54,912 KB |
ソースコード
#include <string> #include <vector> #include<iostream> #include<cstdio> #include<cstdlib> #include<stack> #include<queue> #include<cmath> #include<algorithm> #include<functional> #include<list> #include<deque> #include<bitset> #include<set> #include<map> #include<unordered_map> #include<unordered_set> #include<cstring> #include<sstream> #include<complex> #include<iomanip> #include<numeric> #include<cassert> #include<random> #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X)) #define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X)) #define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X)) #define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)),(X).end()) #define Endl endl #define NL <<"\n" using namespace std; using ll=long long; using pii=pair<int,int>; using pll=pair<ll,ll>; template<class T> using vv=vector<vector<T>>; template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;} template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;} //#undef NUIP #ifdef NUIP #include "benri.h" #else #define out(args...) #endif #ifdef __cpp_init_captures template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);} template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));} #endif const ll MOD=1e9+7; //998244353 struct RMQ{ const int INF=MOD; vv<int> mn; int D; void upd(const vector<int> &v){ mn.clear(); D=0; for(int i=v.size();i;i/=2) ++D; mn.resize(D+1,vector<int>(1<<D+1,INF)); rep(i,v.size()) mn[D][i]=v[i]; rrep(i,D)rep(j,1<<D){ int b=(j>>(D-i)|1)<<(D-i); //assert(b!=(1<<D) || !(j>>(D-i)&1)); mn[i][j]=j>>(D-i)&1?get(b,j+1):get(j,b); } } RMQ(){} RMQ(const vector<int> &v){upd(v);} int get(int l,int r){ //[l,r) --r; if(l==r)return mn[D][l]; if(l>r) return INF; int d=__builtin_clz(l^r)+D-31; return min(mn[d][l],mn[d][r]); } }; // Larsson-Sadakane's Suffix array Construction: O(n (log n)^2) struct SA:public vector<int>{ struct SAComp { const int h; const vector<int> &g; SAComp(int h, const vector<int> &g) : h(h), g(g) {} bool operator() (int a, int b) { return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h]; } }; vector<int> inv,lcp; string str; RMQ rmq; SA(){} SA(const string &str):str(str){build(str);} void build(const string &s){ str=s; buildSA(); buildLCP(); rmq.upd(lcp); inv.resize(size()); rep(i,size()) inv[at(i)]=i; } void buildSA(){ int n=str.size(); vector<int> g(n+1),b(n+1); resize(n+1); rep(i,n+1) at(i)=i, g[i]=str[i]; sort(all(*this), SAComp(0,g)); for(int h=1; b[n]!=n; h*=2) { SAComp comp(h,g); sort(all(*this),comp); rep(i,n) b[i+1]=b[i]+comp(at(i),at(i+1)); rep(i,n+1) g[at(i)]=b[i]; } } void buildLCP(){ int n=str.size(); int h=0; vector<int> b(n+1); lcp.resize(n+1); rep(i,n+1) b[at(i)]=i; rep(i,n+1) { if(b[i]){ for (int j=at(b[i]-1); j+h<n && i+h<n && str[j+h]==str[i+h]; ++h); lcp[b[i]]=h; } else lcp[b[i]]=-1; if (h>0) --h; } } int getMn(int l,int r){return rmq.get(l+1,r+1);} }; ostream& operator<<(ostream &os,const SA &sa){ rep(i,sa.size()) os<<sa.lcp[i]<<"\t"<<sa.str.substr(sa[i])<<endl; return os; } void kmp(const string &str,vector<int> &re){ re.resize(str.size()+1); re[0]=-1; int j=-1; rep(i,str.size()){ while(j>=0 && str[i]!=str[j]) j=re[j]; re[i+1]=++j; } } vector<ll> solve(string s,ll m,vector<ll> inds){ const int q=inds.size(); const int n=s.size(); if(m==1){ SA sa(s); vector<ll> re(q); rep(i,q) re[i]=sa[inds[i]+1]; return re; } SA sa(s+s); out(sa,1); vector<pll> ps; ll cur=0; for(auto x:sa){ if(x==2*n) continue; ps.eb(cur,x); if(x<n) cur+=m-1; else ++cur; } ps.eb(cur,-1); assert(cur==n*m); out(ps,1); vector<ll> re(q); ll ad=(m-2ll)*n; rep(i,q){ auto ind=inds[i]; auto p=*(upper_bound(all(ps),pll(ind,MOD))-1); out(p,1); auto dif=ind-p.X; re[i]=p.Y+ad-n*dif; } return re; } vector<ll> naive(string s,ll m,vector<ll> inds){ string ss; rep(_,m) ss+=s; return solve(ss,1,inds); } vector<ll> solve2(string s,ll m,vector<ll> inds){ const int n=s.size(); vector<int> kmp; ::kmp(s,kmp); out(kmp,1); int t=n-kmp.back(); if(kmp.back() && n%t==0){ s=s.substr(0,t); m*=n/t; } out(s,m,1); return solve(s,m,inds); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout<<fixed<<setprecision(0); if(0){ rep(_,10000){ const int n=rand()%10+1; string s(n,'.'); for(char &c:s) c=rand()%1+'a'; // s+=s; s+=s; // s.resize(rand()%s.size()+1); int m=10; vector<ll> inds(s.size()*m); iota(all(inds),0); auto exp=naive(s,m,inds); auto act=solve2(s,m,inds); if(exp!=act){ out(s,m,inds,exp,act,1); return 0; } } return 0; } ll n,m,q; cin>>n>>m>>q; string s; cin>>s; vector<ll> inds(q); for(auto &x:inds) cin>>x, --x; auto re=solve2(s,m,inds); rep(i,q) cout<<re[i]+1<<" \n"[i+1==q]; return 0; }