日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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

總結

以上是生活随笔為你收集整理的【SPOJ】7258. Lexicographical Substring Search(后缀自动机)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。