spoj7258:Lexicographical Substring Search(后缀自动机+拓扑排序)
来源:互联网 发布:js转圈的进度条效果 编辑:程序博客网 时间:2024/05/18 00:31
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:aaaaa
题意:给一个字符串,多次询问字典序大小为kth的本质不同字符串。
题解:后缀自动机。
对串构建SAM并拓扑排序统计每个状态能走到的字符串个数
DP转移方程:f[i]=sigma(f[to[i][j])
询问时贪心即可。
代码:
#include <iostream>#include <cstring>using namespace std;const int Maxn=2e5+50;char ch[Maxn];int n,Q,c[Maxn],que[Maxn];inline int read(){char ch=getchar();int i=0,f=1;while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){i=(i<<3)+(i<<1)+ch-'0';ch=getchar();}return i*f;}struct sam{int to[Maxn][26],par[Maxn],l[Maxn],f[Maxn];int p,np,q,nq,last,num;sam(){last=++num;}inline void extend(int c){p=last;last=np=++num;l[np]=l[p]+1;for(;p&&!to[p][c];p=par[p])to[p][c]=np;if(!p)par[np]=1;else{q=to[p][c];if(l[q]==l[p]+1)par[np]=q;else{nq=++num,l[nq]=l[p]+1;memcpy(to[nq],to[q],sizeof(to[q]));par[nq]=par[q],par[q]=par[np]=nq;for(;p&&to[p][c]==q;p=par[p])to[p][c]=nq;}}}inline void build(){scanf("%s",ch+1);n=strlen(ch+1);for(int i=1;i<=n;i++)extend(ch[i]-'a');}inline void calc(){for(int i=1;i<=num;i++)c[l[i]]++;for(int i=1;i<=n;i++)c[i]+=c[i-1];for(int i=num;i>=1;i--)que[c[l[i]]--]=i;for(int i=num;i>=1;i--){int q=que[i];f[q]=1;for(int j=0;j<26;j++)if(to[q][j])f[q]+=f[to[q][j]];}}inline void query(int kth){int pos=1;while(kth>0){for(int j=0;j<26;j++)if(to[pos][j]){if(f[to[pos][j]]>=kth){pos=to[pos][j];putchar('a'+j);--kth;break;}else kth-=f[to[pos][j]];}}puts("");}}sam;int main(){sam.build();sam.calc();Q=read();while(Q--){int kth=read();sam.query(kth);}}
- spoj7258:Lexicographical Substring Search(后缀自动机+拓扑排序)
- spoj7258 Lexicographical Substring Search(SUBLEX) 后缀自动机
- [spoj7258]Lexicographical Substring Search && 后缀自动机
- spoj7258:Lexicographical Substring Search 后缀自动机
- 【SPOJ7258】Lexicographical Substring Search-后缀自动机+拓补序递推
- spoj7258 Lexicographical Substring Search(SUBLEX),后缀自动机
- [SPOJ7258]SUBLEX - Lexicographical Substring Search(后缀自动机)
- [SPOJ7258]SUBLEX - Lexicographical Substring Search
- spoj 7258 Lexicographical Substring Search(后缀数组 | 后缀自动机)
- SPOJ 7258 SUBLEX - Lexicographical Substring Search (后缀自动机)
- spoj 7258 Lexicographical Substring Search (后缀自动机)
- SPOJ Lexicographical Substring Search --后缀自动机
- SPOJ SUBLEX Lexicographical Substring Search 后缀自动机
- 【后缀自动机】[SPOJ SUBLEX]Lexicographical Substring Search
- SPOJ Lexicographical Substring Search 后缀自动机
- spoj8222:Substring(后缀自动机+拓扑排序)
- spoj 7258 Lexicographical Substring Search (后缀自动机+dp)
- SPOJ 题目7528 SUBLEX - Lexicographical Substring Search(后缀自动机求排名k的子串)
- [12]javascript变量、作用域及内存
- PHP三元运算符的简写形式
- POJ3311 Hie with the Pie 【TSP】【状压dp】
- Excel Sheet Column Number问题及解法
- ADT(Abstract Data Type)
- spoj7258:Lexicographical Substring Search(后缀自动机+拓扑排序)
- Tomcat内部结构及深入性能调优
- Python批量判断IP地址所属地区
- JS和JQuery中如何获取和修改控件显示内容(如lable,div,h1)
- 【源码】H5-高仿jd
- C++ 记录程序运行时间的方法总结
- VewPager.setOnPagerChangListener失效
- 2017最新各大视频网站VIP解析播放,看优酷、腾讯、爱奇艺、...
- [Ubuntu]update-alternatives命令说明简介及用法