题目链接

codeforces.

洛谷.

Solution

按照套路,可以\(SAM\)上线段树合并求出\(endpos\)集合,然后随便贪心一下就好了。

#include<bits/stdc++.h>
using namespace std; void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
} void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double
#define ll long long
#define mid ((l+r)>>1) const int maxn = 4e5+10;
const int inf = 1e9;
const lf eps = 1e-8; int rt[maxn]; namespace sgt { //segment tree
int tot,ls[maxn*20],rs[maxn*20]; void insert(int &p,int l,int r,int x) {
if(!p) p=++tot;
if(l==r) return ;
if(x<=mid) insert(ls[p],l,mid,x);
else insert(rs[p],mid+1,r,x);
} int query(int p,int l,int r,int x,int y) {
if((!p)||x>y) return 0;
if(x<=l&&r<=y) return 1;
int res=0;
if(x<=mid) res|=query(ls[p],l,mid,x,y);
if(y>mid) res|=query(rs[p],mid+1,r,x,y);
return res;
} int merge(int x,int y) {
if(!x||!y) return x^y;
int now=++tot;
ls[now]=merge(ls[x],ls[y]);
rs[now]=merge(rs[x],rs[y]);
return now;
}
} int n,q;
char s[maxn]; namespace sam { //suffix automaton
int tr[maxn][26],par[maxn],ml[maxn],cnt=1,lstp=1,pos[maxn];
void append(int x) {
int p=lstp,np=++cnt;ml[np]=ml[p]+1;lstp=np;
sgt::insert(rt[np],1,n,ml[np]);
for(;p&&tr[p][x]==0;p=par[p]) tr[p][x]=np;
if(!p) return par[np]=1,void();
int q=tr[p][x];
if(ml[p]+1<ml[q]) {
int nq=++cnt;ml[nq]=ml[p]+1;
memcpy(tr[nq],tr[q],sizeof tr[nq]);
par[nq]=par[q],par[q]=par[np]=nq;
for(;p&&tr[p][x]==q;p=par[p]) tr[p][x]=nq;
} else par[np]=q;
} struct Tree {
int head[maxn],tot;
struct edge{int to,nxt;}e[maxn<<1];
void ins(int u,int v) {e[++tot]=(edge){v,head[u]},head[u]=tot;}
void dfs(int x) {
for(int i=head[x];i;i=e[i].nxt)
dfs(e[i].to),rt[x]=sgt::merge(rt[x],rt[e[i].to]);
}
}T; void prepare() {for(int i=2;i<=cnt;i++) T.ins(par[i],i);T.dfs(1);} void solve() {
int l,r;read(l),read(r);scanf("%s",s+1);
int len=strlen(s+1),now=1,lim=len;pos[0]=1;
for(int i=1,v;i<=len;i++)
if(tr[now][v=s[i]-'a']) now=tr[now][v],pos[i]=now;
else {lim=i-1;break;}
int bo=0;
for(int i=lim;~i;i--) {
for(int j=(i==len?0:s[i+1]-'a'+1);j<26;j++)
if(sgt::query(rt[tr[pos[i]][j]],1,n,l+i,r)) {
for(int k=1;k<=i;k++) putchar(s[k]);
putchar(j+'a'),puts(""),bo=1;goto end;
}
}end:;
if(!bo) puts("-1");
}
} int main() {
scanf("%s",s+1);n=strlen(s+1);
for(int i=1;i<=n;i++) sam:: append(s[i]-'a');
sam:: prepare();read(q);while(q--) sam::solve();
return 0;
}

[CF1037H] Security的更多相关文章

  1. CF1037H Security 线段树合并 SAM

    LINK:Security 求一个严格大于T的字符串 是原字符串S[L,R]的子串. 容易想到尽可能和T相同 然后再补一个尽可能小的字符即可. 出于这种思想 可以在SAM上先跑匹配 然后枚举加哪个字符 ...

  2. CF1037H Security——SAM+线段树合并

    又是一道\(SAM\)维护\(endpos\)集合的题,我直接把CF700E的板子粘过来了QwQ 思路 如果我们有\([l,r]\)对应的\(SAM\),只需要在上面贪心就可以了.因为要求的是字典序比 ...

  3. CF1037H Security 后缀自动机 + right集合线段树合并 + 贪心

    题目描述: 给定一个字符串 $S$ 给出 $Q$ 个操作,给出 $L,R,T$,求出字典序最小的 $S_{1}$ 为 $S[L...R]$的子串,且 $S_{1}$ 的字典序严格大于 $T$. 输出这 ...

  4. SAM 做题笔记(各种技巧,持续更新,SA)

    SAM 感性瞎扯. 这里是 SAM 做题笔记. 本来是在一篇随笔里面,然后 Latex 太多加载不过来就分成了两篇. 标 * 的是推荐一做的题目. trick 是我总结的技巧. I. P3804 [模 ...

  5. [笔记] 后缀自动机 (SAM)

    实现 void ins(int c){ int np = ++dcnt, p = lst; lst = np; t[np].len = t[p].len + 1, t[np].eps = 1; whi ...

  6. Security Policy:行级安全(Row-Level Security)

    行级安全RLS(Row-Level Security)是在数据行级别上控制用户的访问,控制用户只能访问数据库表的特定数据行.断言是逻辑表达式,在SQL Server 2016中,RLS是基于安全断言( ...

  7. Content Security Policy 入门教程

    阮一峰文章:Content Security Policy 入门教程

  8. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  9. WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题

    摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication ...

随机推荐

  1. 2 进程multiprocessing [mʌltɪ'prəʊsesɪŋ] time模块

    1.multiprocessing模块 multiprocessing模块就是跨平台版本的多进程模块. multiprocessing模块提供了一个Process类来代表一个进程对象, 2.Proce ...

  2. SpringBoot-01:什么是SpringBoot?

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- SpringBoot: Spring Boot可以轻松创建独立的,生产级的基于Spring的应用程序,您可以“ ...

  3. How To Install Apache Tomcat 7 on CentOS 7 via Yum

    摘自:https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-7-on-centos-7-via-y ...

  4. python generator

    def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('g ...

  5. Python 更换国内pip源

    pip国内的一些镜像: 阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...

  6. lesson 14 A noble gangster

    lesson 14 A noble gangster there was a ++time++ 时期 times 时期/年代 in times of peace a sum of + money 一笔 ...

  7. Python中一些糟糕的语法!你遇到过吗?还知道那些?

    Python是一门语法优雅,功能强大,开发效率高,应用领域广泛的解释性语言. 其有非常多的优点,但是也并不是完美的,除了大家都知道的执行速度不够快,Python2和Python3的兼容问题,以及GIL ...

  8. 【WXS全局对象】JSON

    方法: 原型:JSON.stringify( Object ) 说明:将 object 对象转换为 JSON 字符串,并返回该字符串. 返回:[String] 原型:JSON.parse( [Stri ...

  9. Tensorflow学习第1课——从本地加载MNIST以及FashionMNIST数据

    很多Tensorflow第一课的教程都是使用MNIST或者FashionMNIST数据集作为示例数据集,但是其给的例程基本都是从网络上用load_data函数直接加载,该函数封装程度比较高,如果网络出 ...

  10. LeetCode 96——不同的二叉搜索树

    1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...