SPOJ 题目7528 SUBLEX - Lexicographical Substring Search(后缀自动机求排名k的子串)
2015-09-05 19:53
363 查看
SUBLEX - Lexicographical Substring Search
no tagsLittle 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 singleinteger 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 这个oj我也是醉了,,刚开始还是用习惯的指针形式写后缀自动机,一直re,我以为太慢了,就换用数组的试了试,一直TLE,题目是输入一组,我就吧输入多组改成输入一组,提交wa了,没有超时,然后就想是不是之前的代码也行,去掉!=EOF那个提交就过了,然后改了改数组形式的,提交也过了 ac代码 指针形式#include<stdio.h> #include<stdlib.h> #include<string.h> #define max(a,b) (a>b?a:b) #define N 500010 struct sam { sam *pre,*son[26]; int len,g; }que ,*root,*tail,*b ; int tot; void add(int c,int l) { sam *p=tail,*np=&que[tot++]; np->len=l; tail=np; while(p&&p->son[c]==NULL) { p->son[c]=np; p=p->pre; } if(p==NULL) np->pre=root; else { sam *q=p->son[c]; if(p->len+1==q->len) np->pre=q; else { sam *nq=&que[tot++]; *nq=*q; nq->len=p->len+1; np->pre=q->pre=nq; while(p&&p->son[c]==q) { p->son[c]=nq; p=p->pre; } } } } char str ; int cnt ,len; void build() { int i,j; tot=0; root=tail=&que[tot++]; len=strlen(str); for(i=0;i<len;i++) add(str[i]-'a',i+1); memset(cnt,0,sizeof(cnt)); for(i=0;i<tot;i++) cnt[que[i].len]++; for(i=1;i<=len;i++) cnt[i]+=cnt[i-1]; for(i=0;i<tot;i++) b[--cnt[que[i].len]]=&que[i]; for(i=tot-1;i>=0;i--) { sam *p=b[i]; if(b[i]==NULL) continue; p->g=1; for(j=0;j<26;j++) { if(p->son[j]) p->g+=p->son[j]->g; } } } void query(int x) { sam *p=root; int i,j; while(x) { for(i=0;i<26;i++) { if(p->son[i]!=NULL) { if(p->son[i]->g>=x) { printf("%c",'a'+i); p=p->son[i]; x--; break; } else x-=p->son[i]->g; } } } printf("\n"); } int main() { scanf("%s",str); build(); int q; scanf("%d",&q); while(q--) { int x; scanf("%d",&x); query(x); } }
数组形式#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 200020 char str ; int cot,last; int fa ,son [26],len ,b ,n; int g ,cnt ; void add(int c,int l) { int p,np; p=last; np=last=++cot; len[np]=++n; while(p&&!son[p][c]) { son[p][c]=np; p=fa[p]; } if(!p) { fa[np]=1; } else { int q=son[p][c]; if(len[p]+1==len[q]) fa[np]=q; else { int nq=++cot; len[nq]=len[p]+1; memcpy(son[nq],son[q],sizeof(son[q])); fa[nq]=fa[q]; fa[q]=fa[np]=nq; while(son[p][c]==q) { son[p][c]=nq; p=fa[p]; } } } } void build() { cot=last=1; n=0; /* memset(son,0,sizeof(son)); memset(len,0,sizeof(len)); memset(b,0,sizeof(b)); memset(fa,0,sizeof(fa));*/ int i,j; // int Len=strlen(str); for(i=0;str[i];i++) add(str[i]-'a',i+1); for(i=1;i<=cot;i++) { cnt[len[i]]++; } for(i=1;i<=n;i++) { cnt[i]+=cnt[i-1]; } for(i=1;i<=cot;i++) { b[cnt[len[i]]--]=i; } } void init() { int i,j; for(i=cot;i;i--) { g[b[i]]=1; for(j=0;j<26;j++) g[b[i]]+=g[son[b[i]][j]]; } } void query(int x) { int p=1; while(x) { int i; for(i=0;i<26;i++) { if(son[p][i]) { if(g[son[p][i]]>=x) { printf("%c",'a'+i); p=son[p][i]; x--; break; } else x-=g[son[p][i]]; } } } printf("\n"); } int main() { scanf("%s",str); build(); init(); int q; scanf("%d",&q); while(q--) { int x; scanf("%d",&x); query(x); } }
相关文章推荐
- ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
- Going Graphical — The dialog Utility
- 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
- 计划任务作业
- 桶排序及其应用
- HDU - 3081 Marriage Match II(二分+最大流)
- 确保nginx安全的10个技巧
- 一个500人使用的后台服务站点优化过程
- 数据绑定
- 卡尔曼(kalman)滤波器概述
- ListView可以做哪些事情呢
- java结构与算法之选择排序
- DNS处理模块dnspython之详解模块域名解析方法
- HDU 5032 Always Cook Mushroom (2014年北京赛区现场赛A题)
- 搭建nginx反向代理用做内网域名转发
- CDN内容分发网络
- angular中的scope
- Android强制设置横屏或竖屏
- PCB打烊Gerber文件生成方法
- 二元冒泡排序(改进的冒泡算法)