天天看点

SPOJ SUBLEX Lexicographical Substring Search

Description

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

If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?

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

Example:

S = "aaa" (without quotes)

substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:

"a", "aa", "aaa".

Input

In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In 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

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100005;
char s[maxn];
int n, x;

class SAM
{
  const static int maxn = 200005;   //节点个数  
  const static int size = 26;     //字符的范围  
  const static char base = 'a';     //字符的基准  

  class node
  {
  public:
    node *fa, *next[size];
    int len, cnt;
    node* clear(int x)
    {
      fa = 0; len = x;
      cnt = 0;
      memset(next, 0, sizeof(next));
      return this;
    }
  }nd[maxn], *u[maxn];                     //节点的设置  

  node *root, *last;              //根节点,上一个节点  
  int tot, f[maxn], len;                        //总节点数  
public:
  void clear()
  {
    last = root = &nd[len = tot = 0];
    nd[0].clear(0);
  }                               //初始化  

  void insert(char ch)
  {
    len = last->len + 1;
    node *p = last, *np = nd[++tot].clear(p->len + 1);
    last = np;
    int x = ch - base;
    while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa;
    if (p == 0) { np->fa = root; return; }

    node* q = p->next[x];
    if (p->len + 1 == q->len) { np->fa = q; return; }

    node *nq = nd[++tot].clear(p->len + 1);
    for (int i = 0; i < size; i++)
    if (q->next[i]) nq->next[i] = q->next[i];
    nq->fa = q->fa;
    q->fa = np->fa = nq;
    while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa;
  }                               //插入操作  

  void pre()
  {
    for (int i = 0; i <= len; i++) f[i] = 0;
    for (int i = 1; i <= tot; i++) f[nd[i].len]++;
    for (int i = 1; i <= len; i++) f[i] += f[i - 1];
    for (int i = 1; i <= tot; i++) u[f[nd[i].len]--] = &nd[i];
    for (int i = tot; i; i--)
    {
      u[i]->cnt = 1;
      for (int j = 0; j < size; j++)
        if (u[i]->next[j]) u[i]->cnt += u[i]->next[j]->cnt;
    }
  }
  void find(int x)
  {
    node *s = root;
    while (x)
    {
      for (int i = 0; i < size; i++)
      if (s->next[i])
        if (x>s->next[i]->cnt) x -= s->next[i]->cnt;
        else { 
          --x; s = s->next[i]; 
          putchar(i + base); break;
        }
    }
    putchar(10);
  }
}sam;

int main()
{
  //while ( != EOF)
  {
    scanf("%s%d", s, &n);
    sam.clear();
    for (int i = 0; s[i]; i++) sam.insert(s[i]);
    sam.pre();
    while (n--)
    {
      scanf("%d", &x);
      sam.find(x);
    }
  }
  return 0;
}