题目描述:

对于一个给定长度为N的字符串,求它的第K小子串是什么。

样例输入:

aabc
0 3

样例输出:

aab

题解:

构造后缀自动机,然后在后缀自动机上跑dfs

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif #define R register
#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)
#define gmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define gmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1<<15],*S=B,*T=B;
inline int FastIn()
{
R char ch;R int cnt=0;R bool minus=0;
while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ?minus=1:cnt=ch-'0';
while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus?-cnt:cnt;
}
#define maxn 1000010
char str[maxn];
int a[maxn][26] , val[maxn] , fa[maxn] , l[maxn] ,last , tot , p , np , q , nq , t , k ;
int v[maxn] , que[maxn] , sum[maxn] ;
inline void extend (R int c)
{
p = last ;
np = last = ++tot ;
l[np] = l[p] + 1 ;
val[np] = 1;
for ( ; !a[p][c] && p ; ) a[p][c] = np , p = fa[p] ;
if (!p) fa[np] = 1 ;
else
{
q = a[p][c] ;
if (l[p] + 1 == l[q] ) fa[np] = q ;
else
{
nq = ++tot ;
l[nq] = l[p] + 1 ;
memcpy ( a[nq] , a[q] ,sizeof (a[q]) );
fa[nq] = fa[q] ;
fa[np] = fa[q] = nq ;
for ( ; a[p][c]==q ; ) a[p][c] = nq , p = fa[p] ;
}
}
}
inline void prepare()
{
for (R int i = 1 ; i <= tot ; i++ ) v[ l[i] ] ++ ;
for (R int i = 1 ; str[i] ; i++ ) v[i] += v[i-1] ;
for (R int i = tot ; i ; i-- ) que [ v[ l[i] ] -- ] = i ;
for (R int i = tot ; i ; i-- )
{
R int tmp = que[i];
if (t == 1) val[fa[tmp]] += val[tmp] ;
else val[tmp] = 1 ;
}
val[1] = 0 ;
for (R int i = tot ; i ; i--)
{
R int tmp = que[i] ;
sum[tmp] = val[tmp] ;
for (R int j = 0 ; j < 26 ; j++ )
sum[tmp] += sum [ a[tmp][j] ] ;
}
}
void dfs(R int x , R int k)
{
if (k <= val [x] ) return ;
k -= val[x] ;
for (R int i = 0 ; i < 26 ; i++ )
{
if (int tmp = a[x][i] )
{
if (k <= sum[tmp] )
{
printf("%c" , i + 'a' );
dfs(tmp , k);
return ;
}
k -= sum[tmp];
}
}
}
int main()
{
last = ++ tot;
scanf ( "%s\n", str + 1);
scanf( "%d %d" , &t , &k);
for (R int i = 1 ; str[i] ; i++) extend( str[i] - 'a');
prepare();
if ( sum[1] < k) puts("-1") ;
else dfs(1 , k ) ;
return 0;
}
/*
aasdfsdafsdafsadfasdfsadf
0 4
aasd
*/

最新文章

  1. Cocos2dx 3.12 在AndroidStudio上编译配置
  2. VUE JS 使用组件实现双向绑定
  3. 如何解决Oracle RAC 安装集群软件或数据库时无法自动识别节点
  4. js 完成对图片的等比例缩放的方法
  5. 【转载】Spring中DispatcherServlet与ContextLoaderListener的区别
  6. 游戏开发设计模式之命令模式(unity3d 示例实现)
  7. 解决java访问.netWebService的常见问题
  8. [ES7] Object.observe + Microtasks
  9. EditText 默认不获取焦点,弹出软键盘布局变形解决方案
  10. Android TXT文件读写
  11. 如何使用 flannel host-gw backend?- 每天5分钟玩转 Docker 容器技术(62)
  12. HTTP结构
  13. JMockit常用操作
  14. 4月11日java多线程4
  15. python的基本数据类型(一)
  16. ceph部署手册
  17. eval详解
  18. Jersey Client传递中文参数
  19. MVC初级知识之——Routing路由
  20. sql 用xml方式插入数据乱码问题解决方法

热门文章

  1. 11.metasploit辅助模块----基本Exp----ARP欺骗中间人MITM----WordPress破解
  2. 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。
  3. c++ k^1
  4. C#获取当前路径7中方法
  5. 【官网】2019.5.19 CentOS8.0 最新进展
  6. (4.31)sql server中的xml数据操作
  7. Java - PhantomJS + EChartsConvert实现ECharts图片保存到服务端
  8. 模板 - 无旋Treap
  9. ubuntu系统更新命令
  10. C++ 中头文件&lt;bits/stdc++.h&gt;的优缺点