BZOJ5261 Rhyme
传送门
广义后缀自动机= =+
跟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的更多相关文章
- BZOJ2938 [Poi2000]病毒 和 BZOJ5261 Rhyme
[Poi2000]病毒 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码 ...
- Codeforces 792B. Counting-out Rhyme
B. Counting-out Rhyme time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...
- [省选模拟]Rhyme
考的时候脑子各种短路,用个SAM瞎搞了半天没搞出来,最后中午火急火燎的打了个SPFA才混了点分. 其实这个可以把每个模式串长度为$K-1$的字符串看作一个状态,这个用字符串Hash实现,然后我们发现这 ...
- B. Counting-out Rhyme(约瑟夫环)
Description n children are standing in a circle and playing the counting-out game. Children are numb ...
- POJ1671 Rhyme Schemes
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1776 Accepted: 984 Special Judge De ...
- 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 ...
- CodeForce-792B Counting-out Rhyme(模拟)
Counting-out Rhyme CodeForces - 792B 题意: n 个孩子在玩一个游戏. 孩子们站成一圈,按照顺时针顺序分别被标号为 1 到 n.开始游戏时,第一个孩子成为领导. 游 ...
- BZOJ5261 Rhyme--广义SAM+拓扑排序
原题链接,不是权限题 题目大意 有\(n\)个模板串,让你构造一个尽量长的串,使得这个串中任意一个长度为\(k\)的子串都是至少一个模板串的子串 题解 可以先看一下这道题 [POI2000]病毒 虽然 ...
- BZOJ 5261 Rhyme
思路 考虑一个匹配的过程,当一个节点x向后拼接一个c的时候,为了满足题目条件的限制,应该向suflink中最深的len[x]+1>=k的节点转移(保证该后缀拼上一个c之后,长度为k的子串依然属于 ...
随机推荐
- 继续写高精!noip2012国王游戏。。。
国王游戏 题目描述: 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王 ...
- (转)原理到实现 | K8S 存储之 NFS
转:https://mp.weixin.qq.com/s/Mrr1Rnl_594Gyyn9fHekjw 1NFS介绍 NFS是Network File System的简写,即网络文件系统,NFS是Fr ...
- matlab: undocumented sprintfc
今天本想找一个类似于 R 中 paste 的 matlab 函数, 结果在 stackoverflow 上找到一个叫 sprintfc 的函数 (http://stackoverflow.com/qu ...
- java sftp.exec无法执行mv命令
编写java程序过程中,sftp上传下载建目录删除文件都可以,就是备份不行. 分析原因如下: 1.如果用的同一个用户,即sftp用户来通过 exec(ssh连接) 执行mv命令,那极有可能是在搭建sf ...
- 求 主板型号 945GME - ICH7M/U 支持的最大内存,以及内存型号 10
https://zhidao.baidu.com/question/400302290.html 求 主板型号 945GME - ICH7M/U 支持的最大内存,以及内存型号 10 主板型号 明基 J ...
- 设置chrome解决跨域问题
开发中遇到跨域问题, 解决方法一 以关闭安全验证模式启动chrome,就不会报跨域了 开发阶段,用这个方式是可以的,不然他需要后台配置成 * , 发布会有风险 设置方法: --disable-w ...
- Game on a Tree Gym - 102392F(树上最大匹配)
思路: 本质是求一个树上的最大匹配能否覆盖所有的点. dfs遍历,用qian[]数组记录当前节点的子树内有几个没有匹配的点(初始化为-1因为可以匹配掉一个子树中未匹配的点),pipei[]数组记录当前 ...
- Android深度探索-卷1第七章心得体会
创建LED驱动的设备文件 第一步:使用cdev_init函数初始化cdev 第二步:指定设备号.直接在代码指定或动态分配 第三步:使用cdev_add函数将字符设备添加到内核中的字符设备数组中 第四步 ...
- Mac002--Git安装
Mac--Git安装 注意:在安装Git前,可先安装homebrew,应用brew命令安装Git即可. 一.Mac 安装homebrew 参考博客:https://blog.csdn.net/yuex ...
- tf.nn.conv2d卷积函数之图片轮廓提取
一.tensorflow中二维卷积函数的参数含义:def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_for ...