题目链接:点击查看

题目大意:给出一个长度为 nnn 的字符串 sss,对于每个前缀来说,求出字典序最大的子串。

题目分析:看到子串的字典序,感觉能用后缀树来做,参考了一下大佬的赛上代码: 香港中文大学(深圳)- 新手上路

需要观察出本题的一个结论就是,答案一定是一个前缀的后缀,所以我们只需要求出每个答案子串的左端点即可。

按照套路,对原串建立后缀树,按照字典序跑出 dfs 序 dfndfndfn,顺便维护一下每个节点首次出现的 endposendposendpos 。

然后就可以用优先队列贪心了。记前缀 pre[i]=s[1]s[2]⋯s[i]pre[i]=s[1]s[2]\cdots s[i]pre[i]=s[1]s[2]⋯s[i],对于 pre[n]pre[n]pre[n] 来说,我们只需要从 parentparentparent 树的叶子结点里,选择 dfndfndfn 最大的那个叶子结点作为答案即可。

而对于 pre[n−1]pre[n-1]pre[n−1] 来说,需要将过期的结点淘汰,更具体的,如果一个结点所代表的字符串,已经不在 pre[n−1]pre[n-1]pre[n−1] 所表示的前缀中出现,我们需要在逻辑上删掉这个叶子结点,同时判断一下其父节点是否会成为一个新的叶子结点。

时间复杂度 O(nlog⁡n)O(n\log n)O(nlogn),感觉随便就可以卡掉。。

代码:

// Problem: String Problem
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/24346/M
// Memory Limit: 1048576 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
char s[N];
vector<int>node[N<<1];
int tot,last,endpos[N<<1];
int dfn[N<<1],du[N<<1],cnt;
int ans[N];
struct Node
{int ch[26];int fa,len;
}st[N<<1];
inline int newnode() {tot++;for(int i=0;i<26;i++)st[tot].ch[i]=0;st[tot].fa=st[tot].len=0;endpos[tot]=0;node[tot].clear();return tot;
}
void add(int x) {int p=last,np=last=newnode();st[np].len=st[p].len+1;while(p&&!st[p].ch[x])st[p].ch[x]=np,p=st[p].fa;if(!p)st[np].fa=1;else{int q=st[p].ch[x];if(st[p].len+1==st[q].len)st[np].fa=q;else{int nq=newnode();st[nq]=st[q]; st[nq].len=st[p].len+1;st[q].fa=st[np].fa=nq;while(p&&st[p].ch[x]==q)st[p].ch[x]=nq,p=st[p].fa;//向上把所有q都替换成nq}}
}
void dfs_pos(int u) {for(auto v:node[u]) {dfs_pos(v);endpos[u]=max(endpos[u],endpos[v]);}
}
void dfs(int u) {dfn[u]=++cnt;sort(node[u].begin(),node[u].end(),[&](int x,int y) {return s[endpos[x]-st[u].len]<s[endpos[y]-st[u].len];});for(auto v:node[u]) {dfs(v);}
}
void build() {for(int i=1;i<=tot;i++) {node[st[i].fa].push_back(i);du[st[i].fa]++;}dfs_pos(1);dfs(1);
}
void init() {last=1;cnt=tot=0;newnode();
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);scanf("%s",s+1);init();int n=strlen(s+1);reverse(s+1,s+1+n);for(int i=1;i<=n;i++) {add(s[i]-'a');endpos[last]=i;}build();priority_queue<pair<int,int>>q;for(int i=1;i<=tot;i++) {if(!du[i]) {q.push({dfn[i],i});}}for(int i=n;i>=1;i--) {while(1) {int u=q.top().second;int fa=st[u].fa;int l=n-endpos[u]+1;int r=l+st[fa].len;if(r>i) {q.pop();if(--du[fa]==0&&fa!=1) {q.push({dfn[fa],fa});}} else {ans[i]=l;break;}}}for(int i=1;i<=n;i++) {printf("%d %d\n",ans[i],i);}return 0;
}

2021ICPC(沈阳) - String Problem(后缀树+贪心)相关推荐

  1. HDU - 5008 Boring String Problem(后缀树求本质不同第k大子串)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串,再给出 mmm 次询问,每次询问需要输出本质不同第 kkk 小的子串的起止位置.如果有多个答案,输出起点最小的那个.强制在线. 题目分析 ...

  2. hdu 5008 Boring String Problem(后缀数组+rmq)

    题目链接:hdu 5008 Boring String Problem 题意: 给你一个字符串,有q个询问,每次询问该字符串所有的子串中字典序第k小的是哪个串,输出位置,如果有多个位置,输出最靠左的那 ...

  3. HDU 5008 Boring String Problem ( 后缀数组求本质不同第k大子串)

    Boring String Problem Zeronera题解 预处理sum数组记录不同字符串的个数,即sum[i] = n- sa[i] + 1 -height[i] + sum[i-1] (n为 ...

  4. HDU - 5008 Boring String Problem(后缀数组+二分)

    题目链接:点击查看 题目大意:给出一个字符串,接下来给出 q 个询问,每次询问字符串中第 k 大的子串,要求输出该字串的左右端点,如果有多个答案,输出左端点最小的一个 题目分析:因为在求出后缀数组后, ...

  5. 2021CCPC(桂林) - Suffix Automaton(后缀树+线段树)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串,再给出 qqq 次询问,每次询问需要输出本质不同第 kkk 小的子串的起止位置.如果有多个答案,输出起点最小的那个. 本题规定字符串大小 ...

  6. 2018 ICPC 南京 M. Mediocre String Problem(ExKMP + Manacher / ExKMP+回文树)

    2018 ICPC 南京 全文见:https://blog.csdn.net/qq_43461168/article/details/112796538 M. Mediocre String Prob ...

  7. HDU 6194 String String String (后缀数组+线段树, 2017 ACM/ICPC Asia Regional Shenyang Online)

    Problem 求字符串 S 中严格出现 k 次的子串个数 k≥1k\ge 1 |S|≤105|S|\le 10^5 ∑|S|≤2×106\sum |S| \le 2\times 10^6 Idea ...

  8. 2021ICPC沈阳区域赛BEFIJM

    2021ICPC沈阳区域赛BEFIJM E. Edward Gaming, the Champion 题意 给定长度不超过2e52\mathrm{e}52e5的文本串,统计其中"edgnb& ...

  9. Problem M. Mediocre String Problem(Z 函数 + PAM)

    Problem M. Mediocre String Problem 给定两个串s,ts, ts,t,要求有多少不同的三元组(i,j,k)(i, j, k)(i,j,k),满足: 1≤i≤j≤∣s∣1 ...

最新文章

  1. AI聚变:寻找2018最佳人工智能应用案例
  2. python 快速行进 算法 图像修补
  3. 本人常用的基础 linux命令
  4. Eclipse Neon安装指导
  5. 使用 PHP 7 给 Web 应用加速
  6. 深度学习笔记(38) 非极大值抑制
  7. Redis入门第一篇【介绍、安装】
  8. 解开Future的神秘面纱之任务执行
  9. System.currentTimeMillis()与日期之间的相互转换
  10. Excel里如何更改坐标轴起始位置使图落在正中心
  11. MATLAB怎么做出三叶玫瑰线,matlab复习题
  12. 科技公司亚马逊名字由来_名字叫什么? 为什么亚马逊的“认可”是可爱营销的灾难性尝试
  13. 直接下载Google Play上APP的安装包
  14. Chrome插件开发(chrome-extension)
  15. 搭建单节点ELK日志收集
  16. 深度评测阿里云、腾讯云和华为云
  17. 他向导师下跪,仍被强制退学!5年博士白读,双方各执一词,同门师兄也有回应……...
  18. 心跳信号分类---(中)
  19. 互联网摸鱼日报(2022-12-23)
  20. 中国丝光棉市场产销需求及未来前景趋势预测报告(2022-2027年)

热门文章

  1. FineReport连接mysql8.0.16
  2. 自我引用(Self reference)
  3. Nacos源码NacosNamingService
  4. 索引使用原则-列的离散(sàn)度
  5. 通过ObjectProvider进行依赖查找
  6. 古典密码学-古典密码破解方式
  7. Junit_@Before@After
  8. 文件下载--服务器端编程操作
  9. Azkaban-two_server模式-安装3和启动运行
  10. RocketMQ的核心概念讲解