【SPOJ】7258. Lexicographical Substring Search(后缀自动机)
http://www.spoj.com/problems/SUBLEX/
后缀自动机系列完成QAQ。。。撒花。。明天or今晚写个小结?
首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一个子串,而且不重复。(原因似乎是逆序后缀树?
所以我们在自动机上预处理每一个状态的子串数目,然后从小到大枚举字符。
子串数目可以这样预处理出:s[x]=sum{s[y]}+1, y是x出发的下一个点,意思就是说,以x开头的子串有那么多个(即将孩子的所有子串前边都加上x),然后x单独算一个子串。
然后查找的时候从root出发(你可以这样想,因为root的right值包含了所有right,即root有所有后缀的r下标,所以只需要找最小的开头即可,然后转移后同理。
然后如果当前的转移的状态y,有s[y]>=k,那么说明子串在y的后缀里边,那么--k并且打印字符y后转移到y状态。(--k是因为我们转移的定义)
反之,如果s[y]<k, k-=s[y]
然后就行了。。
#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=getchar(); 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, 26) sz[p]+=sz[c[p][x]]; } } void getans(int k) { int now=root; while(k) { rep(x, 26) 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 have 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 will be the K-thsmallest?
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