当前位置:网站首页>[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;
}
边栏推荐
猜你喜欢
Abstract classes and interfaces (study notes)
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
抽象类和接口(学习笔记)
Apache Doris系列之:安装与部署详细步骤
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
WSL安装图形界面并通过xrdp/X-Launch访问
【无标题】
$\text{ARC 145}$
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
OpenCV笔记(二十):滤波函数——filter2D
随机推荐
mysql获取当前时间
【微信小程序】小程序突破小程序二维码数量限制
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
ZZULIOJ:1119: 数列有序
【Summary】机器人方法汇总
【MySQL】Mysql事务以及权限管理
language code table
d违反常了吗
Rust编译报错:error: linker `cc` not found
【LeetCode】55. 跳跃游戏 - Go 语言题解
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
Gxlcms有声小说系统/小说听书系统源码
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
Navicat cannot connect to mysql super detailed processing method
通过对抗性知识蒸馏压缩深度图神经网络
解决一个Mysql的utf8编码导致的问题
MySQL进阶sql性能分析
Chapter 8 Intermediate Shell Tools II