結果

問題 No.1018 suffixsuffixsuffix
ユーザー 👑 kmjpkmjp
提出日時 2020-04-04 03:11:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 2,851 ms
コンパイル使用メモリ 218,656 KB
実行使用メモリ 18,172 KB
最終ジャッジ日時 2023-09-16 05:17:37
合計ジャッジ時間 7,644 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 60 ms
5,224 KB
testcase_10 AC 60 ms
5,196 KB
testcase_11 AC 62 ms
5,416 KB
testcase_12 AC 55 ms
5,124 KB
testcase_13 AC 54 ms
5,192 KB
testcase_14 AC 137 ms
17,360 KB
testcase_15 AC 141 ms
18,172 KB
testcase_16 AC 146 ms
17,892 KB
testcase_17 AC 128 ms
17,664 KB
testcase_18 AC 132 ms
17,668 KB
testcase_19 AC 23 ms
4,376 KB
testcase_20 AC 23 ms
4,380 KB
testcase_21 AC 24 ms
4,376 KB
testcase_22 AC 24 ms
4,376 KB
testcase_23 AC 25 ms
4,376 KB
testcase_24 AC 145 ms
17,500 KB
testcase_25 AC 151 ms
18,136 KB
testcase_26 AC 137 ms
17,784 KB
testcase_27 AC 134 ms
17,512 KB
testcase_28 AC 134 ms
17,588 KB
testcase_29 AC 30 ms
4,376 KB
testcase_30 AC 29 ms
4,380 KB
testcase_31 AC 29 ms
4,380 KB
testcase_32 AC 30 ms
4,376 KB
testcase_33 AC 28 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 23 ms
4,376 KB
testcase_36 AC 27 ms
4,376 KB
testcase_37 AC 143 ms
18,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll N,M,Q;
string S;
int per;

template<typename ST=string>
struct SuffixArray {
	int N; vector<int> rank,lcp,sa,rsa; ST S;

	SuffixArray(ST S) : S(S){
		int i,h=0; vector<int> tmp;
		N=S.size(); rank.resize(N+1); sa.resize(N+1); tmp.resize(N+1);
		FOR(i,N+1) sa[i]=i, rank[i]=i==N?-1:S[i];
		
		for(int k=1; k<=N; k<<=1) {
			auto pred2 = [k,this](int& a,int &b)->bool{ return (((a+k<=N)?rank[a+k]:-1)<((b+k<=N)?rank[b+k]:-1));};
			auto pred = [pred2,k,this](int& a,int &b)->bool{ return (rank[a]!=rank[b])?(rank[a]<rank[b]):pred2(a,b);};
			int x=0;
			if(k!=1) for(i=1;i<N+1;i++) if(rank[sa[i]]!=rank[sa[x]]) sort(sa.begin()+x,sa.begin()+i,pred2), x=i;
			sort(sa.begin()+x,sa.end(),pred);
			FOR(i,N+1) tmp[sa[i]]=(i==0)?0:tmp[sa[i-1]]+pred(sa[i-1],sa[i]);
			swap(rank,tmp);
		}
		lcp.resize(N+1); rsa.resize(N+1);
		FOR(i,N+1) rsa[sa[i]]=i;
		FOR(i,N) {
			int j=sa[rsa[i]-1];
			for(h=max(h-1,0);i+h<N && j+h<N; h++) if(S[j+h]!=S[i+h]) break;
			lcp[rsa[i]-1]=h;
		}
	}
};



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>Q>>S;
	for(i=1;i<N;i++) if(N%i==0) {
		FOR(j,N) if(j>=i && S[j-i]!=S[j]) break;
		if(j==N) {
			S=S.substr(0,i);
			M*=N/i;
			N=i;
			
			break;
		}
	}
	
	if(M==1) {
		SuffixArray sa(S);
		FOR(i,Q) {
			cin>>x;
			cout<<1+sa.sa[x]<<" ";
		}
		cout<<endl;
		return;
	}
	
	SuffixArray sa(S+S);
	vector<vector<ll>> V;
	ll pos=0;
	FOR(i,2*N) {
		if(sa.sa[i+1]>=N) {
			V.push_back({pos,1LL*(M-1)*N+sa.sa[i+1]-N,1});
		}
		else {
			V.push_back({pos,sa.sa[i+1],M-1});
		}
		pos+=V.back()[2];
	}
	
	FOR(i,Q) {
		ll v;
		cin>>v;
		v--;
		vector<ll> C={v+1,0,0};
		x=lower_bound(ALL(V),C)-V.begin();
		x--;
		cout<<(V[x][1]+1+(V[x][2]-1-(v-V[x][0]))*N)<<" ";
	}
	cout<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0