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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[SPOJ7258]Lexicographical Substring Search

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [SPOJ7258]Lexicographical Substring Search 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[SPOJ7258]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".

輸入

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 consists of?Q?lines, the?i-th?contains a string which is the answer to the?i-th?asked question.

輸入示例

aaa 2 2 3

輸出示例

aa aaa

題解

這題其實就是這道題的一個子問題。

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> using namespace std;int read() {int x = 0, f = 1; char c = getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }return x * f; }#define maxn 90010char S[maxn]; int n, rank[maxn], height[maxn], sa[maxn], Ws[maxn];bool cmp(int* a, int p1, int p2, int l) {if(p1 + l > n && p2 + l > n) return a[p1] == a[p2];if(p1 + l > n || p2 + l > n) return 0;return a[p1] == a[p2] && a[p1+l] == a[p2+l]; } void ssort() {int *x = rank, *y = height;int m = 0;for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = max(m, x[i]);for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];for(int i = n; i; i--) sa[Ws[x[i]]--] = i;for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) {pos = 0;for(int i = n - j + 1; i <= n; i++) y[++pos] = i;for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;for(int i = 1; i <= m; i++) Ws[i] = 0;for(int i = 1; i <= n; i++) Ws[x[i]]++;for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];swap(x, y); pos = 1; x[sa[1]] = 1;for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;}return ; } void calch() {for(int i = 1; i <= n; i++) rank[sa[i]] = i;for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k)for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++);return ; }int en[maxn];int main() {scanf("%s", S + 1);n = strlen(S + 1);ssort();calch();for(int i = 1; i <= n; i++) en[i] = n - sa[i] + 1 - height[i];for(int i = 1; i <= n; i++) en[i] += en[i-1];int q = read();while(q--) {int k = read(), p = lower_bound(en + 1, en + n + 1, k) - en;int l = sa[p], r = n - (en[p] - k);for(int i = l; i <= r; i++) putchar(S[i]); putchar('\n');}return 0; }

這道題用后綴自動機也是可以滴。對于每個節點 i 我們維護一發 f(i) 表示狀態 i 之后總共有多少種填法,然后我們每一位枚舉填哪個字母,看后續狀態數是否 ≥ k。

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> using namespace std;int read() {int x = 0, f = 1; char c = getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }return x * f; }#define maxn 180010 #define maxa 26char S[maxn]; int n;int rt, last, ToT, ch[maxn][maxa], par[maxn], Max[maxn], f[maxn]; void extend(int x) {int p = last, np = ++ToT; Max[np] = Max[p] + 1; f[np] = 1; last = np;while(p && !ch[p][x]) ch[p][x] = np, p = par[p];if(!p){ par[np] = rt; return ; }int q = ch[p][x];if(Max[q] == Max[p] + 1){ par[np] = q; return ; }int nq = ++ToT; Max[nq] = Max[p] + 1; f[nq] = 1;memcpy(ch[nq], ch[q], sizeof(ch[q]));par[nq] = par[q];par[q] = par[np] = nq;while(p && ch[p][x] == q) ch[p][x] = nq, p = par[p];return ; } int sa[maxn], Ws[maxn]; void build() {for(int i = 1; i <= ToT; i++) Ws[n-Max[i]]++;for(int i = 1; i <= n; i++) Ws[i] += Ws[i-1];for(int i = ToT; i; i--) sa[Ws[n-Max[i]]--] = i;for(int i = 1; i <= ToT; i++)for(int c = 0; c < maxa; c++) f[sa[i]] += f[ch[sa[i]][c]];return ; }int main() {scanf("%s", S);n = strlen(S);rt = last = ToT = 1;for(int i = 0; i < n; i++) extend(S[i] - 'a');build(); // for(int i = 1; i <= ToT; i++) printf("%d%c", f[i], i < ToT ? ' ' : '\n');int q = read();while(q--) {int k = read(), p = rt;while(k)for(int c = 0; c < maxa; c++) if(ch[p][c])if(f[ch[p][c]] >= k) {putchar(c + 'a'); k--; p = ch[p][c]; break;}else k -= f[ch[p][c]];putchar('\n');}return 0; }

?

轉載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6544662.html

總結

以上是生活随笔為你收集整理的[SPOJ7258]Lexicographical Substring Search的全部內容,希望文章能夠幫你解決所遇到的問題。

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