当前位置:网站首页>[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;
}
边栏推荐
- Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
- Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
- Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题
- “由于找不到MSVCP140.dll,无法继续执行代码,重新安装程序可能会解决此问题等”解决方案
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- Apache Doris series: detailed steps for installation and deployment
- ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
- 【无标题】
- PhpMetrics 使用
- VS2017编译Tars测试工程
猜你喜欢
# Dasctf 7月赋能赛 WP
代码越写越乱?那是因为你没用责任链
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
Alibaba Cloud video on demand + project combat
【MySQL】Mysql事务以及权限管理
【LeetCode】70. 爬楼梯 - Go 语言题解
Excel基础学习笔记
Apache Doris系列之:深入认识实时分析型数据库Apache Doris
Apache Doris series: detailed steps for installation and deployment
$\text{ARC 145}$
随机推荐
Apache Doris series: detailed steps for installation and deployment
d使用among的问题
成功解决ModuleNotFoundError: No module named ‘Image‘
HF2022-EzPHP reproduction
Navicat cannot connect to mysql super detailed processing method
阿里云视频点播+项目实战
Excel基础学习笔记
Regular expression syntax and usage
反转链表-就地逆置法
BFS题单总结
[MySQL] DQL related operations
Day016 类和对象
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
【MySQL】Mysql事务以及权限管理
OpenCV笔记(二十):滤波函数——filter2D
Detailed operator
【无标题】
【Untitled】
Introducing the visualization tool Netron
Go1.18升级功能 - 泛型 从零开始Go语言