結果

問題 No.1018 suffixsuffixsuffix
ユーザー chocoruskchocorusk
提出日時 2020-04-04 18:12:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 2,343 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 119,500 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2023-09-16 06:02:55
合計ジャッジ時間 11,240 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,500 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 208 ms
4,420 KB
testcase_10 AC 221 ms
4,500 KB
testcase_11 AC 222 ms
4,604 KB
testcase_12 AC 209 ms
4,376 KB
testcase_13 AC 209 ms
4,404 KB
testcase_14 AC 328 ms
7,640 KB
testcase_15 AC 321 ms
7,488 KB
testcase_16 AC 339 ms
7,716 KB
testcase_17 AC 315 ms
7,528 KB
testcase_18 AC 318 ms
7,448 KB
testcase_19 AC 158 ms
4,380 KB
testcase_20 AC 161 ms
4,376 KB
testcase_21 AC 161 ms
4,376 KB
testcase_22 AC 166 ms
4,380 KB
testcase_23 AC 166 ms
4,376 KB
testcase_24 AC 339 ms
7,408 KB
testcase_25 AC 362 ms
7,656 KB
testcase_26 AC 330 ms
7,360 KB
testcase_27 AC 330 ms
7,416 KB
testcase_28 AC 321 ms
7,676 KB
testcase_29 AC 186 ms
4,376 KB
testcase_30 AC 186 ms
4,376 KB
testcase_31 AC 186 ms
4,384 KB
testcase_32 AC 183 ms
4,384 KB
testcase_33 AC 178 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 168 ms
4,376 KB
testcase_36 AC 180 ms
4,376 KB
testcase_37 AC 341 ms
7,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct SuffixArray{
	int n, k;
	vector<int> rk, tmp, sa;
	string s;
	SuffixArray(string s):n(s.size()), s(s), rk(s.size()+1), tmp(s.size()+1), sa(s.size()+1){
		auto compare_sa=[&](int i, int j){
			if(rk[i]!=rk[j]) return rk[i]<rk[j];
			else{
				int ri, rj;
				if(i+k<=n) ri=rk[i+k];
				else ri=-1;
				if(j+k<=n) rj=rk[j+k];
				else rj=-1;
				return ri<rj;
			}
		};
		for(int i=0; i<=n; i++){
			sa[i]=i;
			if(i<n){
				rk[i]=s[i]-'A';
			}else{
				rk[i]=-1;
			}
		}
		for(k=1; k<=n; k<<=1){
			sort(sa.begin(), sa.end(), compare_sa);
			tmp[sa[0]]=0;
			for(int i=1; i<=n; i++){
				tmp[sa[i]]=tmp[sa[i-1]];
				if(compare_sa(sa[i-1], sa[i])) tmp[sa[i]]++;
			}
			rk=tmp;
		}
	}
	int operator[](const int &k) const{
		return sa[k];
	}
};
vector< int > z_algorithm(const string &s) {
  vector< int > prefix(s.size());
  for(int i = 1, j = 0; i < s.size(); i++) {
    if(i + prefix[i - j] < j + prefix[j]) {
      prefix[i] = prefix[i - j];
    } else {
      int k = max(0, j + prefix[j] - i);
      while(i + k < s.size() && s[k] == s[i + k]) ++k;
      prefix[i] = k;
      j = i;
    }
  }
  prefix[0] = (int) s.size();
  return prefix;
}
int main()
{
	int n, q;
	ll m;
	cin>>n>>m>>q;
	string s;
	cin>>s;
	if(m==1){
		SuffixArray sa(s);
		while(q--){
			ll k; cin>>k;
			cout<<sa[k]+1<<" ";
		}
		cout<<endl;
		return 0;
	}
	auto z=z_algorithm(s);
	int d;
	for(d=1; d<n; d++){
		if(n%d!=0) continue;
		if(z[d]==n-d) break;
	}
	s=s.substr(0, d);
	m*=n/d;
	n=d;
	ll c=0;
	SuffixArray sa(s+s);
	ll x[200020];
	x[0]=0;
	for(int i=1; i<=2*n; i++){
		x[i]=x[i-1]+((sa[i]<n)?(m-1):1);
	}
	while(q--){
		ll k; cin>>k;
		int j=lower_bound(x, x+2*n+1, k)-x;
		ll ans;
		if(sa[j]>=n){
			ans=sa[j]-n+n*(m-1)+1;
		}else{
			ans=n*(x[j]-k)+1+sa[j];
		}
		cout<<ans<<" ";
	}
	cout<<endl;
	return 0;
}
0