传送门

后缀自动机基础题。

给两个字符串,让你求长度不小于kkk的公共子串的数量。


这题可以用后缀自动机解决废话

考虑对其中一个字串建出后缀自动机,然后用另一个在上面跑,注意到如果一个状态有贡献的话,从它到根的状态都会有贡献,因此我们给每个节点打一个懒标记最后再统计一次答案即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ri register int
using namespace std;
const int N=2e5+5;
typedef long long ll;
int k,n;
char s[N];
inline int calc(char c){return c>='a'&&c<='z'?c-'a':c-'A'+26;}
struct SAM{
	int tot,rt,last,len[N],link[N],son[N][60],siz[N],lz[N],cnt[N],rk[N];
	inline void init(){
		memset(len,0,sizeof(len)),memset(siz,0,sizeof(siz)),memset(rk,0,sizeof(rk)),memset(son,0,sizeof(son));
		memset(link,0,sizeof(link)),memset(cnt,0,sizeof(cnt)),memset(lz,0,sizeof(lz)),tot=last=rt=1,len[0]=-1;
	}
	inline void expend(int x){
		int p=last,np=++tot;
		siz[last=np]=1,len[np]=len[p]+1;
		while(p&&!son[p][x])son[p][x]=np,p=link[p];
		if(!p){link[np]=rt;return;}
		int q=son[p][x],nq;
		if(len[q]==len[p]+1){link[np]=q;return;}
		len[nq=++tot]=len[p]+1,memcpy(son[nq],son[q],sizeof(son[q])),link[nq]=link[q];
		while(p&&son[p][x]==q)son[p][x]=nq,p=link[p];
		link[q]=link[np]=nq;
	}
	inline void topsort(){
		for(ri i=1;i<=tot;++i)++cnt[len[i]];
		for(ri i=1;i<=last;++i)cnt[i]+=cnt[i-1];
		for(ri i=1;i<=tot;++i)rk[cnt[len[i]]--]=i;
		for(ri i=tot;i;--i)siz[link[rk[i]]]+=siz[rk[i]];
	}
	inline void query(){
		topsort();
		int p=1,nowlen=0;
		ll ans=0;
		for(ri i=1;i<=n;++i){
			int x=calc(s[i]);
			if(son[p][x])p=son[p][x],++nowlen;
			else{
				while(p&&!son[p][x])p=link[p];
				if(!p)p=1,nowlen=0;
				else nowlen=len[p]+1,p=son[p][x];
			}
			if(nowlen>=k){
				ans+=(ll)(nowlen-max(k,len[link[p]]+1)+1)*siz[p];
				if(len[link[p]]>=k)++lz[link[p]];
			}
		}
		for(ri i=tot;i;--i){
			p=rk[i];
			ans+=(ll)lz[p]*siz[p]*(len[p]-max(k,len[link[p]]+1)+1);
			if(len[link[p]]>=k)lz[link[p]]+=lz[p];
		}
		cout<<ans<<'\n';
	}
}sam;
int main(){
	while(scanf("%d",&k),k){
		scanf("%s",s+1),n=strlen(s+1),sam.init();
		for(ri i=1;i<=n;++i)sam.expend(calc(s[i]));
		scanf("%s",s+1),n=strlen(s+1),sam.query();
	}
	return 0;
}

2018.12.15 poj3415 Common Substrings(后缀自动机)的更多相关文章

  1. 2018.12.15 hdu4641 K-string(后缀自动机)

    传送门 后缀自动机基础题. 题意简述:支持动态在串尾插入字符,查询在串中出现超过kkk次的子串的个数. 动态修改samsamsam,每次增量构造好了之后在parentparentparent树上从新建 ...

  2. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  3. POJ3415 Common Substrings(后缀数组 单调栈)

    借用罗穗骞论文中的讲解: 计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来.先将两个字符串连起来,中间用一个没有出现过的字符隔开.按height ...

  4. poj3415 Common Substrings (后缀数组+单调队列)

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9414   Accepted: 3123 Description A sub ...

  5. 2018.12.22 bzoj3277: 串(后缀自动机+启发式合并)

    传送门 跟这道题是一模一样的. 于是本蒟蒻又写了一遍10min1A庆祝 代码: #include<bits/stdc++.h> #define ri register int using ...

  6. 2018.12.22 bzoj3473: 字符串(后缀自动机+启发式合并)

    传送门 调代码调的我怀疑人生. 启发式合并用迭代写怎么都跑不过(雾 换成了dfsdfsdfs版本的终于过了233. 题意简述:求给出nnn个字串,对于每个给定的字串求出其有多少个字串在至少kkk个剩下 ...

  7. Tencent Cloud Developers Conference(2018.12.15)

    时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店

  8. 2018.12.15 spoj Substrings(后缀自动机)

    传送门 后缀自动机基础题. 求长度为iii的子串出现次数的最大值. 对原串建出samsamsam,然后用sizsizsiz更新每个maxlenmaxlenmaxlen的答案. 然后由于后缀链接将其转化 ...

  9. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

随机推荐

  1. 【转】iOS 自动化性能采集

      前言 对于iOS总体生态是比较封闭的,相比Android没有像adb这种可以查看内存.cpu的命令.在日常做性能测试,需要借助xcode中instruments查看内存.cpu等数据. 但是借助i ...

  2. oracle数据库新建库

    1.https://jingyan.baidu.com/article/76a7e409f3f37bfc3b6e15de.html 利用工具新建库. 2.创建时报错 out of memory 调节s ...

  3. httpclient和httpUrlConnect区别

    HttpURLConnection的用法 一.创建HttpURLConnection对象 URL url = new URL("http://localhost:8080/TestHttpU ...

  4. Mastering Unity 2D Game Development

    Mastering Unity 2D Game Development will give your game development skills a boost and help you begi ...

  5. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)

    "Oh, There is a bipartite graph.""Make it Fantastic."X wants to check whether a ...

  6. [剑指Offer]23-链表中环的入口节点

    题目链接 https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&t ...

  7. OC 开发规范指南 - 个人见解写的很好

    纽约时报 移动团队 Objective-C 规范指南 这份规范指南概括了纽约时报 iOS 团队的代码约定. 介绍 关于这个编程语言的所有规范,如果这里没有写到,那就在苹果的文档里: • Objecti ...

  8. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树来回遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  9. shell脚本计算斐波那契数列

    计算斐波那契数列 [1,1,2,3,5,8,,,,,] #!/bin/bash n=$ num=( ) i= while [[ $i -lt $n ]] do let num[$i]=num[$i-] ...

  10. 交叉编译python2.7.7

    一.python下载网址: http://www.python.org/ftp/python/ 二.python的交叉编译依赖openssl ,openssl的下载网址: https://www.op ...