[BZOJ4566][HAOI2016]找相同子串
用SAM重新写了一遍..
我的方法比较笨,先把两个串连在一起,算出来相同子串个数,同理算出s1和s2的子串个数。作差即可。
至于如何统计子串个数,首先toposort后搞出right集的大小,然后$C_{|right|}^{2} \times (step[i]-step[fa])$就是答案。
//BZOJ 4565
//by Cydiater
//2017.1.21
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
#define cmax(a,b) a=max(a,b)
#define cmin(a,b) a=min(a,b)
#define FILE "find_2016"
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
char s1[MAXN],s2[MAXN],s[MAXN];
int len1,len2;
struct SAM{
int now,cnt,son[MAXN][30],step[MAXN],pre[MAXN],rnk[MAXN],label[MAXN];
ll g[MAXN],ans;
SAM(){now=1;cnt=1;}
void Extend(int nxt){
int p=now,np=++cnt;now=np;g[np]=1;
step[np]=step[p]+1;
for(;p&&!son[p][nxt];p=pre[p])son[p][nxt]=np;
if(!p)pre[np]=1;
else{
int q=son[p][nxt],nq;
if(step[q]==step[p]+1)pre[np]=q;
else{
step[(nq=++cnt)]=step[p]+1;
memcpy(son[nq],son[q],sizeof(son[q]));
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
}
}
}
void Build(int len){
up(i,1,len)Extend(s[i]-'a');
}
void Clear(){
now=cnt=1;
memset(pre,0,sizeof(pre));
memset(son,0,sizeof(son));
memset(step,0,sizeof(step));
memset(g,0,sizeof(g));
memset(label,0,sizeof(label));
memset(rnk,0,sizeof(rnk));
}
ll col(int len){
//topsort
ans=0;
up(i,1,cnt)label[step[i]]++;
up(i,1,len)label[i]+=label[i-1];
up(i,1,cnt)rnk[label[step[i]]--]=i;
down(i,cnt,2){
int node=rnk[i];
if(g[node]>=2)
ans+=(g[node]*(g[node]-1)>>1)*(ll)(step[node]-step[pre[node]]);
g[pre[node]]+=g[node];
}
return ans;
}
}sam;
namespace solution{
void Prepare(){
scanf("%s",s1+1);
len1=strlen(s1+1);
scanf("%s",s2+1);
len2=strlen(s2+1);
}
void Solve(){
ll ans1,ans2,ans3;
memcpy(s,s1,sizeof(s));
sam.Build(len1);
ans1=sam.col(len1);
sam.Clear();
memcpy(s,s2,sizeof(s));
sam.Build(len2);
ans2=sam.col(len2);
sam.Clear();
memcpy(s,s1,sizeof(s));
s[len1+1]='a'+26;
up(i,len1+2,len1+len2+1)s[i]=s2[i-len1-1];
sam.Build(len1+len2+1);
ans3=sam.col(len1+len2+1);
//cout<<ans1<<' '<<ans2<<' '<<ans3<<endl;
ans3-=ans1+ans2;
cout<<ans3<<endl;
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
using namespace solution;
Prepare();
Solve();
return 0;
}
看了看别人用SAM实现的代码,发现其实不必要那样。对于s1建一个sam,统计每个点的路径对应多少个子串。然后s2串在SAM上跑,经过一个点就统计上父亲点的sum(因为变换了位置)再加上当前对应的字符串的数量就行了。
[BZOJ4566][HAOI2016]找相同子串的更多相关文章
- BZOJ4566 [Haoi2016]找相同字符【SAM】
BZOJ4566 [Haoi2016]找相同字符 给定两个字符串\(s和t\),要求找出两个字符串中所有可以相互匹配的子串对的数量 首先考虑可以怎么做,我们可以枚举\(t\)串的前缀\(t'\),然后 ...
- 字符串(后缀数组):HAOI2016 找相同子串
[HAOI2016]找相同子串 [题目描述] 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. [输入格式] 两行,两个字符 ...
- [BZOJ4566][Haoi2016]找相同字符 后缀自动机+dp
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1212 Solved: 694[Submit][Stat ...
- [Bzoj4566][Haoi2016]找相同字符(广义后缀自动机)
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 861 Solved: 495[Submit][Statu ...
- BZOJ4566: [Haoi2016]找相同字符
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- BZOJ4566 [Haoi2016]找相同字符 字符串 SAM
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4566.html 题目传送门 - BZOJ4566 题意 给定两个字符串 $s1$ 和 $s2$ ,问有 ...
- BZOJ4566 Haoi2016 找相同字符【广义后缀自动机】
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- BZOJ4566:[HAOI2016]找相同字符(SAM)
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- BZOJ4566 [Haoi2016]找相同字符 【后缀数组】
题目 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. 输入格式 两行,两个字符串s1,s2,长度分别为n1,n2.1 & ...
随机推荐
- ios 将图片做成圆形
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- Ibatis的#和$的区别
来自别人的:https://blog.csdn.net/findmyself_for_world/article/details/49976555 总结:凡是#的,都作为参数,用setobject方式 ...
- 简述泛型、用Maven创建Web项目以及在Web项目上整合SpringMVC
表设计 Timestamp列是否取消"根据当前时间戳自动更新" 是否null及默认值选择合理不合理 外键命名规范及更新和删除时的动作是否合理 泛型 类型参数 --允许在外部指定 ...
- javascript自动识别是否移动设备访问
代码 JavaScript | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function is_pc(){ var os = new Array(& ...
- Cookies and Caching Client Identification
w HTTP The Definitive Guide 11.6.9 Cookies and Caching You have to be careful when caching documents ...
- Big Data资料汇总
整理和翻新一下自己看过和笔记过的Big Data相关的论文和Blog Streaming & Spark In-Stream Big Data Processing Discretized S ...
- python基础-第七篇-7.3反射
定义 反射是根据字符串的形式去对操作其成员 了解反射前,我先看看内置方法__import__,还记得这个内置方法吗? __import__ 用于以字符串的形式导入模块 inp = input('请输 ...
- 15.Update Documents-官方文档摘录
1.插入数据 db.inventory.insertMany( [ { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: ...
- return的结果
return只会返回第一个,接下去的不会再返回 所以return放在for里面用的话,即使循环好几次,但是除了循环的第一个,后面循环出来的结果都作废