传送门

解题思路

  感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\)。考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次,然后求个后缀和,这个可以并查集来实现。具体就是记一个\(sum[i][3]\)表示第\(i\)个联通块中\(0,1,2\)的个数,\(0,1,2\)就是出现在三个串的哪一个,然后合并时需要容斥一下。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std;
const int N=300005;
const int MOD=1e9+7; char s[N];
int fa[N],sum[N][3],n,m,Len,id[N],height[N],ans[N],type[N]; struct SA{
int X[N<<1],Y[N<<1],rk[N],sa[N],c[N],num;
void get_SA(){
for(int i=1;i<=n;++i) X[i]=s[i],c[X[i]]++;
for(int i=2;i<=m;++i) c[i]+=c[i-1];
for(int i=n;i;--i) sa[c[X[i]]--]=i;
for(int k=1;k<=n;k<<=1){ num=0;
for(int i=n-k+1;i<=n;++i) Y[++num]=i;
for(int i=1;i<=n;++i) if(sa[i]>k) Y[++num]=sa[i]-k;
memset(c,0,sizeof(c));
for(int i=1;i<=n;++i) c[X[i]]++;
for(int i=2;i<=m;++i) c[i]+=c[i-1];
for(int i=n;i;--i) sa[c[X[Y[i]]]--]=Y[i],Y[i]=0;
swap(X,Y); X[sa[1]]=1; num=1;
for(int i=2;i<=n;++i)
X[sa[i]]=(Y[sa[i]]==Y[sa[i-1]] && Y[sa[i]+k]==Y[sa[i-1]+k])?num:++num;
m=num; if(m==n) break;
}
}
void get_height(){
for(int i=1;i<=n;++i) rk[sa[i]]=i;
int j,k=0;
for(int i=1;i<=n;++i){
if(rk[i]==1) continue;
if(k) k--; j=sa[rk[i]-1];
while(j+k<=n && i+k<=n && s[i+k]==s[j+k]) k++;
height[rk[i]]=k;
}
}
}t; inline bool cmp(int x,int y){
return height[x]>height[y];
} int get(int x){
if(x==fa[x]) return x;
return fa[x]=get(fa[x]);
} int main(){
char ss[N]; int len;
scanf("%s",ss+1); len=strlen(ss+1); Len=len;
for(int i=1;i<=len;++i) s[++n]=ss[i],type[n]=1;
s[++n]='#'; type[n]=-1;
scanf("%s",ss+1); len=strlen(ss+1); Len=min(Len,len);
for(int i=1;i<=len;++i) s[++n]=ss[i],type[n]=2;
s[++n]='#'+1; type[n]=-1;
scanf("%s",ss+1); len=strlen(ss+1); Len=min(Len,len);
for(int i=1;i<=len;++i) s[++n]=ss[i],type[n]=3;
m='z'; t.get_SA(); t.get_height();
for(int i=1;i<=n;i++) id[i]=i,fa[i]=i;
for(int i=1;i<=n;i++){
if(type[i]==1) sum[i][0]=1;
if(type[i]==2) sum[i][1]=1;
if(type[i]==3) sum[i][2]=1;
}
sort(id+1,id+1+n,cmp); int now=1,tmp=0,u,v;
for(int i=Len;i;i--){
while(now<=n && height[id[now]]>=i) {
u=get(t.sa[id[now]-1]),v=get(t.sa[id[now]]);
tmp-=(1ll*sum[u][0]*sum[u][1]%MOD*sum[u][2]%MOD);
tmp=(tmp+MOD)%MOD;
tmp-=(1ll*sum[v][0]*sum[v][1]%MOD*sum[v][2]%MOD);
tmp=(tmp+MOD)%MOD;
sum[u][0]=(sum[u][0]+sum[v][0])%MOD;
sum[u][1]=(sum[u][1]+sum[v][1])%MOD;
sum[u][2]=(sum[u][2]+sum[v][2])%MOD;
tmp=tmp+(1ll*sum[u][0]*sum[u][1]%MOD*sum[u][2]%MOD)%MOD;
tmp%=MOD; now++; fa[v]=u;
}
ans[i]=tmp;
}
for(int i=1;i<=Len;i++) printf("%d ",ans[i]);
return 0;
}

CF 452E. Three strings(后缀数组+并查集)的更多相关文章

  1. BZOJ 4566 JZYZOJ 1547 [haoi2016T5]找相同子串 后缀数组 并查集

    http://172.20.6.3/Problem_Show.asp?id=1547 http://www.lydsy.com/JudgeOnline/problem.php?id=4566 单纯后缀 ...

  2. NOI 2015 品酒大会 (后缀数组+并查集)

    题目大意:略 40分暴力还是很好写的,差分再跑个后缀和 和 后缀最大值就行了 一种正解是后缀数组+并查集 但据说还有后缀数组+单调栈的高端操作蒟蒻的我当然不会 后缀数组求出height,然后从大到小排 ...

  3. [UOJ#131][BZOJ4199][NOI2015]品酒大会 后缀数组 + 并查集

    [UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...

  4. Uva 12361 File Retrieval 后缀数组+并查集

    题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...

  5. BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )

    求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...

  6. 【学术篇】NOI2015 品酒大会 后缀数组+并查集

    省选前大致是刷不了几道题了... 所以就找一些裸一点的题目练练板子算了= = 然而这题一点都不裸, 也并不怎么好写... 于是就浪费了将近一下午的时间... 然而还不是因为后缀数组板子不熟= = 首先 ...

  7. POJ 3415 Common Substrings 后缀数组+并查集

    后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height   求完之后按height值从大往小合并.  height值代表的是  sa[i]和sa[i ...

  8. 4199. [NOI2015]品酒大会【后缀数组+并查集】

    Description 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加.在大会的晚餐上,调酒师 ...

  9. 【BZOJ4199】[Noi2015]品酒大会 后缀数组+并查集

    [BZOJ4199][Noi2015]品酒大会 题面:http://www.lydsy.com/JudgeOnline/wttl/thread.php?tid=2144 题解:听说能用SAM?SA默默 ...

随机推荐

  1. hdu6699Block Breaker

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  2. 2019/10/13 TZOJ

    水题虽不好,但是很爽 渴望未来某天能把剩下的题补了,先做个记录. Hard Disk Drive http://acm.hdu.edu.cn/showproblem.php?pid=4788 单位转化 ...

  3. 20190814 On Java8 第四章 运算符

    第四章 运算符 优先级 程序员经常都会忘记其他优先级规则,所以应该用括号明确规定运算顺序. 赋值 基本类型的赋值都是直接的,而不像对象,赋予的只是其内存的引用.举个例子,a = b ,如果 b 是基本 ...

  4. mybatise根据list参数查询

    where id in<foreach item="item" index="index" collection="map.idList&quo ...

  5. 两数相加(java版本)

    (一).单链表实现 package com.lin.leetcode.addTwoNumbers; /** * Created by Yaooo on 2019/8/26. */ public cla ...

  6. 安装weblogic界面安装

    如果中间有任何问题请联系作者:291562721 界面安装weblogic首先 需要工具: 链接:https://pan.baidu.com/s/1x3uYxsnycjT2Xi2TOTbDdQ 提取码 ...

  7. 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(二)

    前言 已完成数据预处理工作,具体参照: 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(一) 设置配置文件 新建目录face_faster_rcn ...

  8. 浏览器报406 错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers

    The resource identified by this request is only capable of generating responses with characteristics ...

  9. Shell03---流程控制

    目录 Shell03---流程控制 1. 流程控制语句if基本概述 2. 流程控制语句if文件比较 3. 流程控制语句if整数比较 4. 流程控制语句if字符比较 5. 流程控制语句if正则比较 6. ...

  10. 【LeetCode】几何学 geometry(共2题)

    [587]Erect the Fence [892]Surface Area of 3D Shapes