zoukankan      html  css  js  c++  java
  • SPOJ SUBLEX

    SUBLEX - Lexicographical Substring Search

    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

    Edited: Some input file contains garbage at the end. Do not process them.

    解法

    后缀数组解法

    先建一个SAM之后贪心跑就好了。

    #include <cstdio>
    #include <cstring>
    
    struct SAM {
    	static const int MaxS = 90000 * 2 + 15;
    	struct node {
    		int step, cnt;
    		node *pre, *ch[26];
    		void clear() {
    			cnt = 0;
    			pre = NULL;
    			memset(ch, 0, sizeof ch);
    		}
    	} node_mset[MaxS], *order[MaxS], *cnode, *last, *root;
    	int cnt[MaxS], size;
    	
    	inline node *newnode(const int &step) {
    		++size;
    		cnode->clear();
    		cnode->step = step;
    		return cnode++;
    	}
    
    	void Init() {
    		size = 0;
    		cnode = node_mset;
    		last = root = newnode(0);
    	}
    
    	void Insert(const int &c) {
    		node *p = last, *np = newnode(p->step + 1);
    		for (; p && !p->ch[c]; p = p->pre)
    			p->ch[c] = np;
    		if (!p)
    			np->pre = root;
    		else {
    			node *q = p->ch[c];
    			if (p->step + 1 == q->step)
    				np->pre = q;
    			else {
    				node *nq = newnode(p->step + 1);
    				memcpy(nq->ch, q->ch, sizeof q->ch);
    				nq->pre = q->pre;
    				np->pre = q->pre = nq;
    				for (; p && p->ch[c] == q; p = p->pre)
    					p->ch[c] = nq;
    			}
    		}
    		last = np;
    	}
    
    	void toposort() {
    		for (int i = 0; i < size; ++i) cnt[i] = 0;
    		for (node *it = node_mset; it < cnode; ++it) ++cnt[it->step];
    		for (int i = 1; i < size; ++i) cnt[i] += cnt[i - 1];
    		for (node *it = node_mset; it < cnode; ++it) order[--cnt[it->step]] = it;
    	}
    
    	void calc() {
    		int i, j;
    		node *u, *p;
    		for (i = size - 1; ~i; --i) {
    			u = order[i];
    			for (j = 0; j < 26; ++j)
    				if (u->ch[j]) {
    					p = u->ch[j];
    					u->cnt += p->cnt + 1;
    				}
    		}
    	}
    
    	void getans(int k, char *s) {
    		char *i = s;
    		node *u = root;
    		while (k) {
    			for (int j = 0; j < 26; ++j) {
    				if (!u->ch[j]) continue;
    				if (u->ch[j]->cnt + 1 < k) {
    					k -= u->ch[j]->cnt + 1;
    					continue;
    				}
    				--k;
    				*i++ = j + 'a';
    				u = u->ch[j];
    				break;
    			}
    		}
    		*i = 0;
    	}
    	
    }sam;
    
    
    char s[90005];
    
    int main() {
    
    	sam.Init();
    	scanf("%s", s);
    	for (char *i = s; *i; ++i)
    		sam.Insert(*i - 'a');
    	sam.toposort();
    	sam.calc();
    	
    	int m, k;
    	scanf("%d", &m);
    	while (m--) {
    		scanf("%d", &k);
    		sam.getans(k, s);
    		puts(s);
    	}
    	
    	return 0;
    }
    
    
  • 相关阅读:
    数据结构小练习
    【BZOJ 3652】大新闻 数位dp+期望概率dp
    【BZOJ 3326】[Scoi2013]数数 数位dp+矩阵乘法优化
    【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
    【BZOJ 4455】 [Zjoi2016]小星星 容斥计数
    凸包小结
    Matrix-Tree定理题表
    PLAN OF HEOI(unfinished)
    (ex)BSGS题表
    exBSGS板子
  • 原文地址:https://www.cnblogs.com/cycleke/p/6439104.html
Copyright © 2011-2022 走看看