Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char str[MX];
- int sa[MX], rk[MX], buckets[MX], lcp[MX], gl2[MX];
- int tmp[MX], pos[MX], st[MX * 2][40], n;
- void comp_SA(){
- int mx = 260;
- for(int i = 1; i<=n; i++){
- sa[i] = i;
- rk[i] = str[i];
- }
- for(int i = 1; i >> 1 < n; i <<= 1){
- int k = i >> 1, p = k;
- for(int j = 1; j<=k; j++) buckets[j] = n - (j - 1);
- for(int j = 1; j<=n; j++) if(sa[j] > k) buckets[++p] = sa[j] - k;
- for(int j = 1; j<=n; j++) pos[j] = 0;
- for(int j = 1; j<=n; j++) pos[rk[j]]++;
- for(int j = 1; j<=mx; j++) pos[j] += pos[j - 1];
- for(int j = n; j; j--) sa[pos[rk[buckets[j]]]--] = buckets[j];
- for(int j = 1; j<=n; j++) tmp[sa[j]] = tmp[sa[j - 1]] + (rk[sa[j]] != rk[sa[j - 1]] || rk[sa[j] + k] != rk[sa[j - 1] + k]);
- for(int j = 1; j<=n; j++) rk[j] = tmp[j];
- mx = rk[sa[n]];
- if(mx == n) break;
- }
- }
- void make_lcp(){
- for(int i = 1; i<=n; i++){
- if(rk[i] == 1) cont;
- lcp[rk[i]] = max(0, lcp[rk[i - 1]] - 1);
- for(; str[i + lcp[rk[i]]] == str[sa[rk[i] - 1] + lcp[rk[i]]]; lcp[rk[i]]++); /
- }
- }
- int main(){
- cin.tie(0) -> sync_with_stdio(0);
- cin >> (str + 1);
- n = strlen(str + 1);
- ll k; cin >> k;
- comp_SA();
- make_lcp();
- for(int i = 1; i<=n; i++){
- int dist = (n - (sa[i] + lcp[i])) + 1;
- if(dist < k){
- k -= dist;
- }else{
- for(int j = sa[i]; j < sa[i] + lcp[i] + k; j++){
- cout << str[j];
- }
- cout << "\n";
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement