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

后缀自动机,输入千万不要用while,数据有毒

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值