传送门

广义后缀自动机= =+

跟ptx大爷的博客学的 戳我传送

我写的第一种 建立Trie树的写法 bfs建立SAM

为什么是bfs呢 我也不知道(GG) 经过我一番抱大腿+询问 各位大爷说的原因是 因为dfs时间复杂度不对

多有道理哦 【摔

不过好像这个复杂度保证好像真的不大准确2333

所以 安安心心bfs就最吼啦 复杂度貌似是trie的大小

其实 每次重置last为rt也可以啊qwq 这个复杂度是字符串总长度的

那么这个题的做法就是 建立完SAM 然后 我们类似AC自动机一样建立类fail指针一样的东西

这个指针要保证len>=k 并且两个可以连接起来 也就是x向parent上跳 并且x和上面的串长都要是>=k的 然后找到有这个字符的地方链接就好啦

那么最后我们要求的就是这个新图的最长路

如果出现环了当然就是INF了qwq

附代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define inf 20021225
#define ll long long
#define mxn 100010
using namespace std; struct node{int fa,len,ch[26],c;};
struct edge{int to,lt;}e[mxn*20];
int cnt,in[mxn*4],k;
void add(int x,int y)
{
e[++cnt].to=y;e[cnt].lt=in[x];in[x]=cnt;
}
struct trie
{
node t[mxn];int rt,poi;
void init()
{
for(int i=1;i<=poi;i++) memset(t[i].ch,0,sizeof(t[i].ch)),t[i].fa=t[i].c=0;
rt=poi=1;
}
void insert(char *ch)
{
int n=strlen(ch),pos=rt;
for(int i=0;i<n;i++)
{
int tmp=ch[i]-'a';
if(!t[pos].ch[tmp])
t[pos].ch[tmp]=++poi,t[poi].fa=pos,t[poi].c=tmp;
pos=t[pos].ch[tmp];
}
}
}tr;
struct SAM
{
node t[mxn*4]; int poi,rt,lt[mxn*4];
queue<int> que;
void init()
{
memset(lt,0,sizeof(lt));
for(int i=1;i<=poi;i++) memset(t[i].ch,0,sizeof(t[i].ch)),t[i].fa=t[i].c=t[i].len=0;
poi=0;rt=++poi;
}
int insert(int c,int lt)
{
int p=lt,np=++poi; t[np].len=t[lt].len+1;
for(;p&&!t[p].ch[c];p=t[p].fa) t[p].ch[c]=np;
if(!p){t[np].fa=rt;return np;}
int q=t[p].ch[c];
if(t[q].len==t[p].len+1){t[np].fa=q;return np;}
int nq=++poi; t[nq].len=t[p].len+1;
memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
t[nq].fa=t[q].fa; t[np].fa=t[q].fa=nq;
for(;p&&t[p].ch[c]==q;p=t[p].fa) t[p].ch[c]=nq;
return np;
}
void build()
{
init();
while(!que.empty()) que.pop();
for(int i=0;i<26;i++)
if(tr.t[rt].ch[i]) que.push(tr.t[rt].ch[i]);
lt[tr.rt]=rt;
while(!que.empty())
{
int pos=que.front(); que.pop();
//printf("%d \n",pos);
lt[pos]=insert(tr.t[pos].c,lt[tr.t[pos].fa]);
for(int i=0;i<26;i++)
if(tr.t[pos].ch[i]) que.push(tr.t[pos].ch[i]);
}
}
void makemap()
{
for(int i=1;i<=poi;i++)
if(t[i].len>=k)
{
for(int c=0;c<26;c++)
{
if(t[i].ch[c])
{
if(t[t[i].ch[c]].len>=k) add(t[i].ch[c],i);
}
else
{
int p=i;
while(p&&!t[p].ch[c]&&t[p].len>=k) p=t[p].fa;
int np=t[p].ch[c];// printf("%d %d %d %d\n",i,p,np,t[np].len);
if(p&&np&&t[p].len+1>=k) t[i].ch[c]=np,add(np,i);
}
}
}
}
void print()
{
for(int i=1;i<=poi;i++)
{
printf("id=%d:%d %d\n",i,t[i].fa,t[i].len);
for(int j=0;j<26;j++)
if(t[i].ch[j]) printf("*%d %d\n",j,t[i].ch[j]);
}
}
}sam;
int f[mxn*4]; bool vis[mxn*4];
char ch[mxn];
int search(int pos)
{
//printf("%d %d\n",pos,f[pos]);
if(vis[pos]) return f[pos]=inf;
if(~f[pos]) return f[pos];
f[pos]=sam.t[pos].len; vis[pos]=1;
for(int i=in[pos];i;i=e[i].lt)
{
int y=e[i].to; int tmp=search(y);
if(tmp==inf) return f[pos]=inf;
f[pos] = max(f[pos],tmp+1);
}
vis[pos]=0; return f[pos];
}
void init()
{
for(int i=1;i<=cnt;i++)
e[i].to=e[i].lt=0;
cnt=0;memset(in,0,sizeof(in));
memset(f,-1,sizeof(f));
memset(vis,0,sizeof(vis));
}
int main()
{
int t;
while(~scanf("%d%d",&t,&k))
{
init();
tr.init();//tr.rt=tr.poi=1;
while(t--)
{
scanf("%s",ch);
tr.insert(ch);
}
sam.build();
sam.makemap();
//sam.print();
int ans=k-1;
for(int i=1;i<=sam.poi;i++)
if(sam.t[i].len>=k&&f[i]==-1)
search(i);
for(int i=1;i<=sam.poi;i++) ans=max(ans,f[i]);
if(ans>=inf) printf("INF\n");
else printf("%d\n",ans);
}
return 0;
}

BZOJ5261 Rhyme的更多相关文章

  1. BZOJ2938 [Poi2000]病毒 和 BZOJ5261 Rhyme

    [Poi2000]病毒 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码 ...

  2. Codeforces 792B. Counting-out Rhyme

    B. Counting-out Rhyme time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...

  3. [省选模拟]Rhyme

    考的时候脑子各种短路,用个SAM瞎搞了半天没搞出来,最后中午火急火燎的打了个SPFA才混了点分. 其实这个可以把每个模式串长度为$K-1$的字符串看作一个状态,这个用字符串Hash实现,然后我们发现这 ...

  4. B. Counting-out Rhyme(约瑟夫环)

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...

  5. POJ1671 Rhyme Schemes

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1776   Accepted: 984   Special Judge De ...

  6. 2019上海网络赛 F. Rhyme scheme 普通dp

    Rhyme scheme Problem Describe A rhyme scheme is the pattern of rhymes at the end of each line of a p ...

  7. CodeForce-792B Counting-out Rhyme(模拟)

    Counting-out Rhyme CodeForces - 792B 题意: n 个孩子在玩一个游戏. 孩子们站成一圈,按照顺时针顺序分别被标号为 1 到 n.开始游戏时,第一个孩子成为领导. 游 ...

  8. BZOJ5261 Rhyme--广义SAM+拓扑排序

    原题链接,不是权限题 题目大意 有\(n\)个模板串,让你构造一个尽量长的串,使得这个串中任意一个长度为\(k\)的子串都是至少一个模板串的子串 题解 可以先看一下这道题 [POI2000]病毒 虽然 ...

  9. BZOJ 5261 Rhyme

    思路 考虑一个匹配的过程,当一个节点x向后拼接一个c的时候,为了满足题目条件的限制,应该向suflink中最深的len[x]+1>=k的节点转移(保证该后缀拼上一个c之后,长度为k的子串依然属于 ...

随机推荐

  1. 写一下SPFA和迪杰斯特拉的模版。。。第一次写博客,有错请提出哦!

    SPFA的模版 #include<bits/stdc++.h> using namespace std; queue <int> q; typedef pair <int ...

  2. next_permutation():按字典序输出下一个排列

    #include<iostream> #include<algorithm> using namespace std; int main() { int data[4]={5, ...

  3. [CSP-S模拟测试]:Silhouette(数学)

    题目描述 有一个$n\times n$的网格,在每个格子上堆叠了一些边长为$1$的立方体. 现在给出这个三维几何体的正视图和左视图,求有多少种与之符合的堆叠立方体的方案.两种方案被认为是不同的,当且仅 ...

  4. day14—jQuery UI 之dialog部件

    转行学开发,代码100天——2018-03-30 今天主要展示jQuery UI 之dialog部件的用法, 参考文档:https://jqueryui.com/dialog/ 本文记录分享初始的引用 ...

  5. go tour - Go 入门实验教程

    在线实验地址 - 官网 在线实验地址 - 国内 可以将官方教程作为独立程序在本地安装使用,这样无需访问互联网就能运行,且速度更快,因为是在你的机器上构建并运行代码示例. 本地运行此教程的中文版的步骤如 ...

  6. 基于docker registry镜像安装私服docker hub

    采用docker registry镜像安装docker私服,通过https://hub.docker.com/_/registry链接搜索registry镜像 1.输入命令:docker pull r ...

  7. 关于Nuget包安装之后再卸载,找不到dll的问题

    场景: 在nuget上安装了FFTW.NET,自动的安装一堆其依赖的dll,那些dll都是donet自带的.再卸载这些dll的时候,项目可以生成成功,就是跑不起来.提示如下: 解决方案: 经过排查发现 ...

  8. 在js里的ejs模板引擎使用

    1.首先需要在页面里引用ejs.min.js. 2.将你的模板使用ejs编写,并存成后缀名.stmpl;(可能需要在打包工具里做些处理) 3.在js里使用require引入xxx.stmpl: con ...

  9. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  10. python 模块和包深度学习理解

    python 模块和包 简单说相当于命名空间 1,python 模块        python模块就是一个文件,里面有函数,变量等 import 模块 模块.方法 from 模块 import fu ...