链接

点击跳转

题解

后缀自动机上每条路径都是一个子串

对于t=0t=0t=0,实际上是统计字典序第kkk小的子串,自下向上进行一个dpdpdp就可以解决这个问题

对于t=1t=1t=1,每条路径都被赋予了一个权值,我们可以在一条路径的终点累计这个权值,同样的一个dpdpdp也可以解决问题

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{ll c, f(1);for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;for(;isdigit(c);c=getchar())x=x*10+c-0x30;return f*x;
}
struct SAM
{int tot, las, ch[maxn<<1][26], fa[maxn<<1], len[maxn<<1], pref[maxn<<1];int* operator[](int u){return ch[u];}void init(){int i;rep(i,tot)cl(ch),fa[i]=len[i]=pref[i]=0;tot=las=1;}void append(int c){int p(las);len[las=++tot]=len[p]+1;pref[las]=1;for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;if(!p)fa[las]=1;else{int q=ch[p][c];if(len[q]==len[p]+1)fa[las]=q;else{int qq=++tot;memcpy(ch[qq],ch[q],sizeof(ch[q]));fa[qq]=fa[q];len[qq]=len[p]+1;fa[q]=fa[las]=qq;for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;}}}
}sam;
struct Graph
{int etot, head[maxn], to[maxe], next[maxe], w[maxe];void clear(int N){for(int i=1;i<=N;i++)head[i]=0;etot=0;}void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}#define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
ll cnt[maxn<<1], f[maxn<<1], g[maxn<<1];
bool vis[maxn];
void dp1(int u)
{cnt[u] = sam.pref[u];forp(u,G){dp1(G.to[p]);cnt[u] += cnt[ G.to[p] ];}
}
void dp2(int u)
{if(vis[u])return;vis[u]=1;for(int i=0;i<26;i++)if(sam[u][i]){auto v = sam[u][i];dp2(v);f[u] += 1 + f[v];g[u] += cnt[v] + g[v];}
}
void find0(int u, int res)
{int i;if(res==0)return;for(i=0;i<26;i++)if(sam[u][i]){auto v=sam[u][i];if(1+f[v]>=res){putchar('a'+i);find0(v,res-1);return;}res-=f[v]+1;}
}
void find1(int u, int res)
{int i;if(res<=0)return;for(i=0;i<26;i++)if(sam[u][i]){auto v=sam[u][i];if(cnt[v]+g[v]>=res){putchar('a'+i);find1(v,res-cnt[v]);return;}res-=g[v]+cnt[v];}
}
int n, t, k;
char s[maxn];
int main()
{int i;scanf("%s",s+1); n=strlen(s+1);sam.init();for(i=1;i<=n;i++)sam.append(s[i]-'a');rep(i,sam.tot)if(i>1)G.adde(sam.fa[i],i);dp1(1);dp2(1);t=read(), k=read();if(t==0){if(k>f[1])printf("-1");else find0(1,k);}else{if(k>g[1])printf("-1");else find1(1,k);}return 0;
}

洛谷P3975 [TJOI2015]弦论相关推荐

  1. 洛谷 [P3975 [TJOI2015]弦论

    洛谷 P3975 [TJOI2015]弦论 题目描述 给定一个长度为 nnn 的字符串,求它的第 kkk 小字串:给定 ttt, ttt 为 000 则表示不同位置的相同子串算作一个,ttt 为 11 ...

  2. 洛谷 P3975 [TJOI2015]弦论 解题报告

    P3975 [TJOI2015]弦论 题目描述 为了提高智商,ZJY开始学习弦论.这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为\(n\)的字符串,求 ...

  3. 洛谷P3975 - [TJOI2015]弦论

    Portal Description 给出一个小写字母串\(s(|s|\leq5\times10^5),t\in\{0,1\},k(k\leq10^9)\),求\(s\)的第\(k\)小子串.\(t= ...

  4. 洛谷 - P3975 [TJOI2015]弦论(后缀自动机)

    题目链接:点击查看 题目大意:给出一个字符串 s,再给出一次询问,询问分为两种类型: 0 k:如果不同位置的相同子串算作一个,求第 k 小的子串 1 k:如果不同位置的相同子串算作多个,求第 k 小的 ...

  5. [洛谷P3975][TJOI2015]弦论

    题目大意:求一个字符串的第$k$大字串,$t$表示长得一样位置不同的字串是否算多个 题解:$SAM$,先求出每个位置可以到达多少个字串($Right$数组),然后在转移图上$DP$,若$t=1$,初始 ...

  6. bzoj3998/洛谷3975 [TJOI2015]弦论 (后缀自动机)

    bzoj3998/洛谷3975 [TJOI2015]弦论 题意:求第 k 小字串 方法:先建立SAM,和上一题一样按照拓扑序求出size(表示这个串出现的次数).这道题我们需要求第 k 小,所以我们还 ...

  7. 洛谷_3975 [TJOI2015]弦论(后缀自动机)

    [TJOI2015]弦论 题目链接:https://www.luogu.com.cn/problem/P3975 题解: 对于T==0,只需要构造自动机,将每个状态节点的cnt设为1,然后DFS即可. ...

  8. luogu P3975 [TJOI2015]弦论 SAM

    luogu P3975 [TJOI2015]弦论 链接 bzoj 思路 建出sam. 子串算多个的,统计preant tree的子树大小,否则就是大小为1 然后再统计sam的节点能走到多少串. 然后就 ...

  9. P3975 [TJOI2015]弦论 (SAM)

    [TJOI2015]弦论 - 洛谷 感觉位置不同算相同还是比较好想的,我们top排序后算每个点后面连接了多少点就知道里面有多少字串了,然后求k大就行了: 考了位置不同算不同的话,我们就要考虑endpo ...

最新文章

  1. 再谈Spring Boot中的乱码和编码问题
  2. 阿里云域名备案时产品类型
  3. MySQL binlog日志
  4. MATLAB/simulink控制系统之传递函数模型
  5. 来看看,他的嵌入式之旅!
  6. random()模块随机函数的用法总结
  7. c语言源程序结构是怎样的?
  8. qt中如何模拟按钮点击_如何快速在 Shopify 中加入按钮
  9. 【转】细说.NET中的多线程 (五 使用信号量进行同步)
  10. 教你进入有密码 Windows XP 系统的方法
  11. easyui 扩展验证
  12. linuxpython源文件_Python3 源码安装(Linux 版)
  13. sketch软件_UI设计师必定需要了解的sketch素材
  14. [转载]JXTA概念介绍
  15. java word书签_java 操作word 书签并插入值
  16. android sd卡 f2fs,显著提速!谷歌Pixel 3/3 XL使用了三星F2FS文件系统
  17. http长连接与主动断开方
  18. STM STC NXP单片机 按键扫描
  19. 水文专业对计算机要求,来了来了,高考志愿这么填!
  20. 【推荐】2022年公用事业行业研究报告产业发展前景市场投资行情分析白皮书(附件中为网盘地址,报告持续更新)

热门文章

  1. m基于QPSK调制解调的无线图像传输matlab仿真,包括扩频解扩均衡等模块
  2. 6种电脑录屏工具,免费在线,桌面端Windows和Mac均适用
  3. C语言——大整数加法
  4. WiFi工作原理简述
  5. 戰女神V、ef_latter、BaldrSky 注册表补丁
  6. 在Docker 上完成对Springboot+Mysql+Redis的前后端分离项目的部署(全流程,全截图)
  7. firefox html 拖拽打开网页,Firefox Crash 0Day 一个html网页让你的火狐浏览器崩溃
  8. 一些基础的CSS英语单词
  9. 2种前端实现图片加水印的方式
  10. Leetcode-数据结构-118. 杨辉三角