这题感觉有点坑啊。

题目还是不难想的,先对一个字符串建后缀自动机,然后拿另一个字符串在上面跑。

假设当前跑到了p点,匹配长度为len。

那么当前会对答案产生贡献的串是哪些呢?

显然当前会对p及p到根的链产生贡献。这样显然可以用树形dp优化。

同时需要差分(我也不知道这是否是必须的)。

下面有update。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define mem1(i,j) memset(i,j,sizeof(i))
#define mem2(i,j) memcpy(i,j,sizeof(i))
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
#define poi vec
#define eps 1e-10
#define db double
const int maxn=401000,inf=1000000000,mod=1000000007;
int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0',ch=getchar();}
return f*x;
}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
bool cmin(int& a,int b){return a>b?a=b,true:false;}
struct SAM{
int pre[maxn],c[maxn][27],step[maxn],sa[maxn],cou[maxn],val[maxn],cnt,now,Len;
SAM(){mem1(pre,0);mem1(c,0);mem1(step,0);cnt=now=1;}
int extend(int x){
int np,nq,q,p;
p=now;now=np=++cnt;step[np]=step[p]+1;val[np]++;
while(p&&!c[p][x])c[p][x]=np,p=pre[p];
if(!p)pre[np]=1;
else {
q=c[p][x];
if(step[q]==step[p]+1)pre[np]=q;
else {
step[nq=++cnt]=step[p]+1;
mem2(c[nq],c[q]);
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
while(p&&c[p][x]==q)c[p][x]=nq,p=pre[p];
}
}
}
int getsort(){
up(i,1,cnt)cou[step[i]]++;
up(i,1,cnt)cou[i]+=cou[i-1];
for(int i=cnt;i>=1;i--)sa[cou[step[i]]--]=i;
for(int i=cnt;i>=1;i--)val[pre[sa[i]]]+=val[sa[i]];
}
int walkprepare(){now=1,Len=0;}
int walk(int x){
while(pre[now]&&!c[now][x])now=pre[now],Len=step[now];
if(!c[now][x])return 0;
Len++;now=c[now][x];return Len;
}
int build(char* s){
int n=strlen(s+1);
up(i,1,n)extend(s[i]-'a');
getsort();walkprepare();
}
}a;
char s[maxn];
LL ans[maxn],w[maxn],e[maxn];
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
scanf("%s",s+1);
a.build(s);
scanf("%s",s+1);
int n=strlen(s+1);
up(i,1,n){
int m=a.walk(s[i]-'a');
LL p=a.now;
ans[min(m,a.step[p])]+=a.val[p];
w[p]+=a.val[p];e[p]++;
}
for(int i=a.cnt;i>=1;i--){
ans[a.step[a.sa[i]]]+=e[a.sa[i]]*a.val[a.sa[i]]-w[a.sa[i]];
e[a.pre[a.sa[i]]]+=e[a.sa[i]];
w[a.pre[a.sa[i]]]+=e[a.sa[i]]*a.val[a.sa[i]];//写的丑丑的dp
}
LL sum=0;
for(int i=a.cnt;i>=1;i--)ans[i]+=ans[i+1],sum+=ans[i];
cout<<sum<<endl;
return 0;
}

update:

发现自己的做法麻烦了,我们又不用求长度为i的子串的方案,ans[]完全可以省略,直接预处理到这个节点的方案数,最后加上去即可。

code:修改版本

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define mem1(i,j) memset(i,j,sizeof(i))
#define mem2(i,j) memcpy(i,j,sizeof(i))
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "find_2016"
#define poi vec
#define eps 1e-10
#define db double
const int maxn=401000,inf=1000000000,mod=1000000007;
int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0',ch=getchar();}
return f*x;
}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
bool cmin(int& a,int b){return a>b?a=b,true:false;}
struct SAM{
int pre[maxn],c[maxn][26],step[maxn],sa[maxn],cou[maxn],val[maxn],cnt,now,Len;
LL sum[maxn];
SAM(){mem1(pre,0);mem1(c,0);mem1(step,0);mem1(val,0);mem1(sum,0);cnt=now=1;}
int extend(int x){
int np,nq,q,p;
p=now;now=np=++cnt;step[np]=step[p]+1;val[np]++;
while(p&&!c[p][x])c[p][x]=np,p=pre[p];
if(!p)pre[np]=1;
else {
q=c[p][x];
if(step[q]==step[p]+1)pre[np]=q;
else {
step[nq=++cnt]=step[p]+1;
mem2(c[nq],c[q]);
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
while(p&&c[p][x]==q)c[p][x]=nq,p=pre[p];
}
}
}
int getsort(){
up(i,1,cnt)cou[step[i]]++;
up(i,1,cnt)cou[i]+=cou[i-1];
for(int i=cnt;i>=1;i--)sa[cou[step[i]]--]=i;
for(int i=cnt;i>=1;i--)val[pre[sa[i]]]+=val[sa[i]];
up(i,1,cnt)sum[sa[i]]+=sum[pre[sa[i]]]+(step[sa[i]]-step[pre[sa[i]]])*val[sa[i]];
}
int walkprepare(){now=1,Len=0;}
int walk(int x){
while(pre[now]&&!c[now][x])now=pre[now],Len=step[now];
if(!c[now][x])return 0;
Len++;now=c[now][x];return Len;
}
int build(char* s){
int n=strlen(s+1);
up(i,1,n)extend(s[i]-'a');
getsort();walkprepare();
}
}a;
char s[maxn];
LL ans=0;
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
scanf("%s",s+1);
a.build(s);
scanf("%s",s+1);
int n=strlen(s+1);
up(i,1,n){
int m=a.walk(s[i]-'a');
LL p=a.now;
ans+=a.sum[a.pre[p]]+a.val[p]*(m-a.step[a.pre[p]]);
}
cout<<ans<<endl;
return 0;
}

  

[HAOI2016]找相同子串的更多相关文章

  1. 字符串(后缀数组):HAOI2016 找相同子串

    [HAOI2016]找相同子串 [题目描述] 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. [输入格式] 两行,两个字符 ...

  2. [BZOJ4566][HAOI2016]找相同子串

    COGS传送门 用SAM重新写了一遍.. 我的方法比较笨,先把两个串连在一起,算出来相同子串个数,同理算出s1和s2的子串个数.作差即可. 至于如何统计子串个数,首先toposort后搞出right集 ...

  3. BZOJ 4566: [Haoi2016]找相同字符 [后缀自动机]

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 275  Solved: 155[Submit][Statu ...

  4. bzoj4566 / P3181 [HAOI2016]找相同字符

    P3181 [HAOI2016]找相同字符 后缀自动机 (正解应是广义后缀自动机) 并不会广义后缀自动机. 然鹅可以用普通的后缀自动机.   我们先引入一个问题:算出从一个串内取任意两个不重合子串完全 ...

  5. 【BZOJ4566】[HAOI2016]找相同字符

    [BZOJ4566][HAOI2016]找相同字符 题面 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. 其中\(1\le ...

  6. [BZOJ4566][Haoi2016]找相同字符 后缀自动机+dp

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1212  Solved: 694[Submit][Stat ...

  7. 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈

    [BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...

  8. bzoj 4566 [Haoi2016]找相同字符SA

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 128  Solved: 75[Submit][Status ...

  9. [Bzoj4566][Haoi2016]找相同字符(广义后缀自动机)

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 861  Solved: 495[Submit][Statu ...

随机推荐

  1. Codeforces 934 A.Compatible Pair

    http://codeforces.com/contest/934 A. A Compatible Pair   time limit per test 1 second memory limit p ...

  2. (5)DataSet

    DataTable赋值给DataSet DataSet ds = new DataSet(); DataTable dt1 = new DataTable(); DataTable dt2 = new ...

  3. linux环境设置export

    一. shell显示与设置环境变量 1.export //echo $PATH 2.export | grep ROS 3.export ROS_IP=192.168.0.5(添加环境变量ROS_IP ...

  4. 分享Kali Linux 2017年第12周镜像文件

    分享Kali Linux 2017年第12周镜像文件 Kali Linux官方于3月19日发布2017年的第12周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE. ...

  5. see

    Description 问从点(0,0)能看到点(0,0)和(n,n)之间的矩形的多少个整数点,看到(x,y)代表点(0,0)和点(x,y)间没有其他整数点,如看不到(2,4)因为中间有点(1,2) ...

  6. Excel文件处理Demo

    1.BLL业务逻辑代码 /// <summary> /// 处理“店铺竞品销售数据”导入文件 /// </summary> /// <param name="f ...

  7. ios中表示private

    在.m中写成 如下形式既为私有的形式 @interface ViewController ()  这里只是声明类名和括号即可 /////方法等 @end

  8. xamarin studio 安装

    公司wpf项目移植到mac,用到mono来进行重写,不会,自己开搞 首先一个问题Xamarin怎么读,xaml熟悉吧,xaml读作Zamel,xamarin也就读作Zamerin,恩,就是它了... ...

  9. MinGW在Windows环境下配合命令提示符运行C/C++

    http://jingyan.baidu.com/article/4853e1e5787d6b1909f726f8.html 在电脑中配置MinGW环境. 具体参见我的另一篇分享经验——MinGW在W ...

  10. mysql_config_editor使用简介

      原文 : http://blog.itpub.net/29773961/viewspace-1817640/   ----------------------------------------- ...