日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

bzoj 4566 找相同字符 —— 广义后缀自动机

發布時間:2024/4/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bzoj 4566 找相同字符 —— 广义后缀自动机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:https://www.lydsy.com/JudgeOnline/problem.php?id=4566

建出兩個串的廣義后綴自動機;

統計每個點在兩個串中出現次數的子樹和,其實就是在兩個串中的 right 集合大小;

然后統計答案乘起來即可。

代碼如下:

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int const xn=8e5+5; int n,m,cnt=1,fa[xn],go[xn][30],l[xn],tax[xn],q[xn],d1[xn],d2[xn]; char dc[xn]; int work(int p,int w) {int nq=++cnt,q=go[p][w]; l[nq]=l[p]+1;fa[nq]=fa[q]; fa[q]=nq;memcpy(go[nq],go[q],sizeof go[q]);for(;p&&go[p][w]==q;p=fa[p])go[p][w]=nq;return nq; } int ext(int p,int w) {if(go[p][w]){int q=go[p][w];if(l[q]==l[p]+1)return q; return work(p,w);}int np=++cnt; l[np]=l[p]+1;for(;p&&!go[p][w];p=fa[p])go[p][w]=np;if(!p)fa[np]=1;else {int q=go[p][w];if(l[q]==l[p]+1)fa[np]=q;else fa[np]=work(p,w);}return np; } void rsort() {for(int i=1;i<=cnt;i++)tax[l[i]]++;for(int i=1;i<=cnt;i++)tax[i]+=tax[i-1];for(int i=cnt;i;i--)q[tax[l[i]]--]=i; } int main() {scanf("%s",dc); n=strlen(dc);for(int lst=1,i=0;i<n;i++)lst=ext(lst,dc[i]-'a'+1),d1[lst]++;scanf("%s",dc); m=strlen(dc);for(int lst=1,i=0;i<n;i++)lst=ext(lst,dc[i]-'a'+1),d2[lst]++; rsort();for(int i=cnt,x;i;i--)d1[fa[x=q[i]]]+=d1[x],d2[fa[x]]+=d2[x];ll ans=0;for(int i=1;i<=cnt;i++)ans+=(ll)(l[i]-l[fa[i]])*d1[i]*d2[i];printf("%lld\n",ans);return 0; }

?

轉載于:https://www.cnblogs.com/Zinn/p/10291146.html

總結

以上是生活随笔為你收集整理的bzoj 4566 找相同字符 —— 广义后缀自动机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。