当前位置:网站首页>[SAM template question] P3975 [TJOI2015] string theory
[SAM template question] P3975 [TJOI2015] string theory
2022-07-30 22:57:00 【HeartFireY】
题目思路
On the original string to establish S A M SAM SAM,And then points into cases:
- t = 0 t = 0 t=0,On behalf of the essence is different,那么我们需要对每个 S A M SAM SAM节点 u u u维护 u u uThe number of points to reach the,即为经过 u u uThe nature of the number of different substrings.
- t = 1 t = 1 t=1,On behalf of the position is different,So we consider for each node u u uCalculate number of substring after that point position is different.Then we can think of this need first string s s s的出现次数,也就是 e n d p o s ( s ) endpos(s) endpos(s)的大小,The root to the u u uSaid the list of e n d p o s endpos endpos大小为 p a r e n t t r e e parent\ tree parent tree上 u u uThe number of prefix point in the subtree
不难发现,我们可以维护一个 d p [ u ] dp[u] dp[u]Said the substring under different limit number,And every point, right.对于 t = 0 t = 0 t=0,All right to point point is 1 1 1,否则先 d f s dfs dfsThe right subtree size as point,然后 d f s dfs dfsAnd the answer can be.
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10, MOD = 1e9 + 7;
int dp[N], siz[N];
struct SAM{
int ch[N << 1][26], fa[N << 1], len[N << 1], vis[N << 1];
int last, tot;
SAM(): last(1), tot(1) {
}
inline void extend(int x){
//*Single character extension
int p = last, np = last = ++tot;
len[np] = len[p] + 1, vis[np] = dp[np] = siz[np] = 1;
for(; p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
if(!p) fa[np] = 1;
else{
int q = ch[p][x];
if(len[q] == len[p] + 1) fa[np] = q;
else {
int nq = ++tot;
memcpy(ch[nq], ch[q], sizeof(ch[nq]));
fa[nq] = fa[q], fa[np] = fa[q] = nq, len[nq] = len[p] + 1;
for(; ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
}
}
}
}sam;
vector<int> g[N];
void dfs1(int u){
for(auto v : g[u]){
dfs1(v);
siz[u] += siz[v];
}
dp[u] = siz[u];
}
bool vis[N];
int dfs2(int u){
if(vis[u]) return dp[u];
vis[u] = true;
for(int i = 0; i < 26; i++){
int v = sam.ch[u][i];
if(v) dp[u] += dfs2(v);
}
return dp[u];
}
void build(int t){
for(int i = 1; i <= sam.tot; i++){
if(sam.fa[i]) g[sam.fa[i]].emplace_back(i);
}
if(!t){
for(int i = 1; i <= sam.tot; i++) dp[i] = siz[i] = 1;
} else {
dfs1(1);
}
dp[1] = siz[1] = 0;
dfs2(1);
}
void query(int u, int k){
if(k > dp[u]){
cout << -1;
return;
}
if(k <= siz[u]) return;
k -= siz[u];
for(int i = 0; i < 26; i++){
int v = sam.ch[u][i];
if(k > dp[v]) k -= dp[v];
else{
cout << (char)('a' + i);
query(v, k);
return;
}
}
}
inline void solve(){
string s; cin >> s;
for(int i = 0; i < s.size(); i++) sam.extend(s[i] - 'a');
int t, k; cin >> t >> k;
build(t);
query(1, k);
}
signed main(){
//ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
猜你喜欢

Alibaba Cloud video on demand + project combat

【微信小程序】小程序突破小程序二维码数量限制

go版本升级

ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program

reindex win10

ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序

连号区间数
![[MySQL] Mysql transaction and authority management](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[MySQL] Mysql transaction and authority management

【2022-05-31】JS逆向之易企秀

IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields
随机推荐
Introducing the visualization tool Netron
language code table
【LeetCode】55. 跳跃游戏 - Go 语言题解
【导航规划】导航规划背景知识总结
2022.7.30
MySQL连接时出现2003错误
VS2017编译Tars测试工程
482-静态库、动态库的制作、使用及区别
Abstract classes and interfaces (study notes)
2022中国物流产业大会暨企业家高峰论坛在杭州举办!
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
PhpMetrics 使用
tcp协议传输中的粘包问题
PS Basic Learning (1)
阿里云视频点播+项目实战
Day016 Classes and Objects
【2022-05-31】JS逆向之易企秀
Golang 切片删除指定元素的几种方法
Gxlcms有声小说系统/小说听书系统源码
2022.7.27