"Spoj" 7258. lexicographical Substring Search (suffix automaton)

Source: Internet
Author: User

http://www.spoj.com/problems/SUBLEX/

Suffix Automaton series Complete qaq ... Sprinkle flowers. Tomorrow or write a summary tonight?

The first thing to know: in the suffix automaton, the path from root to any state corresponds to a substring, and is not duplicated. (The reason seems to be the reverse suffix tree?)

So we preprocess the number of substrings in each state on the automaton and then from small to large enumeration characters.

The number of substrings can be preprocessed in this way: S[x]=sum{s[y]}+1, Y is the next point starting with X, meaning that there are so many substrings starting with X (the child's substring is preceded by x), and then X is counted as a substring alone.

Then look for the time from root (you can think, because the right value of root contains all right, that is, Root has all the suffixes of the r subscript, so only need to find the smallest start, and then transfer the same.

Then if the current transferred state Y, there is s[y]>=k, then the substring is inside the suffix of y, then--k and prints the character y after the transition to the Y state. (--k is because of the definition of our transfer)

Conversely, if s[y]<k, K-=s[y]

And then it's OK.

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }struct sam {static const int n=250005;int c[n][26], l[n], F[n], ROot, last, CNT, Sz[n], O[n];sam () {cnt=0; root=last=++cnt;} void Add (int x) {int now=last, a=++cnt; last=a;l[a]=l[now]+1;for (; now &&!c[now][x]; Now=f[now]) c[now][x]=a;if ( !now) f[a]=root;else {int q=c[now][x];if (l[q]==l[now]+1) f[a]=q;else {int b=++cnt;memcpy (c[b], c[q], sizeof c[q]); l[b]= L[now]+1;f[b]=f[q];f[q]=f[a]=b;for (; now && c[now][x]==q; Now=f[now]) c[now][x]=b;}} void Build (char *s) {int Len=strlen (s); Rep (I, Len) Add (s[i]-' a '); For1 (i, 1, CNT) Sz[l[i]]++;for1 (i, 1, len) sz[i]+=sz[i-1] ; For1 (i, 1, CNT) o[sz[l[i]]--]=i;for1 (i, 0, Len) sz[i]=0;for1 (i, 1, CNT) Sz[i]=1;for3 (i, CNT, 1) {int p=o[i];rep (x, N) sz [P]+=sz[c[p][x];}} void Getans (int k) {int now=root;while (k) {Rep (x, +) if (C[now][x]) {int y=c[now][x];if (sz[y]>=k) {Putchar (' a ' +x);--k ; Now=y; Break }else k-=sz[y];}} Puts ("");}} A;const int N=150005;char S[n];int main () {scanf ("%s", s); A.build (s); int q=getint (); while (q--) A.getans (Getint ()); return 0;}

  

Little Daniel loves to play with strings! He always finds different ways to has fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him qquestions of the form:


If all distinct substrings of string S were sorted lexicographically, which one would be the the k-thsmallest?


After knowing the huge number of questions Kinan would ask, Daniel figured out the so he can ' t do this alone. Daniel, of course, knows your exceptional programming skills, so he asked the write him a program which given S would an Swer Kinan ' s questions.

Example:


S = "AAA" (without quotes)
Substrings of S is "a", "a", "a", "AA", "AA", "AAA". The sorted list of substrings would be:
"A", "AA", "AAA".

Input

The first line there is Kinan's string s (with length no more than 90000 characters). It contains only small letters of 中文版 alphabet. The second line contains a single integer q (q <=), the number of questions Daniel would be asked. The next Q lines a single integer K is given (0 < K < 2^31).

Output

Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.

Example
Input:
Aaa
2
2
3

Output:aa
Aaa

"Spoj" 7258. lexicographical Substring Search (suffix automaton)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.