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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 427D Match Catch(后缀数组/广义后缀自动机)

發(fā)布時(shí)間:2024/4/11 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 427D Match Catch(后缀数组/广义后缀自动机) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:點(diǎn)擊查看

題目大意:給出兩個(gè)字符串,求出兩個(gè)字符串中的最短公共子串,且在每個(gè)字符串中只出現(xiàn)過一次

題目分析:因?yàn)檫@個(gè)公共子串只能在字符串中出現(xiàn)一次,考慮到用后綴數(shù)組,我們先將兩個(gè)字符串通過特殊字符拼接起來,求出后綴數(shù)組組后必須滿足的一個(gè)條件就是height[ i ]必須大于height[ i - 1]和height[ i + 1 ]才行,這樣就保證了sa[ i ]和sa[ i - 1 ]的公共子串肯定只出現(xiàn)了一次,其次必須滿足sa[ i ]和sa[ i + 1 ]必須分別位于兩個(gè)字符串之中,滿足以上兩個(gè)條件之后,維護(hù)一下max( height[ i - 1 ] , height[ i + 1 ] ) +?1 的最小值就是答案了

不過這個(gè)題目的特點(diǎn)一個(gè)是多串,一個(gè)是子串,顯然可以用廣義后綴自動(dòng)機(jī)來做,直接將兩個(gè)字符串添加到廣義后綴自動(dòng)機(jī)中,并且每次統(tǒng)計(jì)last的出現(xiàn)次數(shù),拓?fù)湫蚝笾苯咏y(tǒng)計(jì)就好了,顯然后綴自動(dòng)機(jī)處理起來效率更高,且更加無腦

代碼:

后綴數(shù)組:

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e4+100;char str[N];int sa[N]; //SA數(shù)組,表示將S的n個(gè)后綴從小到大排序后把排好序的 //的后綴的開頭位置順次放入SA中 int t1[N],t2[N],c[N];int rk[N],height[N],len,belong[N];int s[N];void build_sa(int s[],int n,int m)//n為添加0后的總長 {int i,j,p,*x=t1,*y=t2;for(i=0;i<m;i++) c[i]=0;for(i=0;i<n;i++) c[x[i]=s[i]]++;for(i=1;i<m;i++) c[i]+=c[i-1];for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;for(j=1;j<=n;j<<=1) {p=0;for(i=n-j;i<n;i++) y[p++]=i;for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;for(i=0;i<m;i++) c[i]=0;for(i=0;i<n;i++) c[x[y[i]]]++;for(i=1;i<m;i++) c[i]+=c[i-1];for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];swap(x,y);p=1,x[sa[0]]=0;for(i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;if(p>=n) break;m=p;} }void get_height(int s[],int n)//n為添加0后的總長 {int i,j,k=0;for(i=0;i<=n;i++)rk[sa[i]]=i;for(i=0;i<n;i++) {if(k) k--;j=sa[rk[i]-1];while(s[i+k]==s[j+k]) k++;height[rk[i]]=k;} }void solve(int base=128) {build_sa(s,len+1,base);get_height(s,len); }int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);len=0;scanf("%s",str);int len1=strlen(str);for(int i=0;str[i];i++){belong[len]=1;s[len++]=str[i];}s[len++]=' ';scanf("%s",str);for(int i=0;str[i];i++){belong[len]=2;s[len++]=str[i];}s[len]=0;solve(); int ans=inf;for(int i=2;i<=len;i++){if(belong[sa[i-1]]+belong[sa[i]]==3){if(height[i-1]<height[i]&&height[i+1]<height[i])ans=min(ans,max(height[i-1],height[i+1])+1);}}if(ans==inf)ans=-1;printf("%d\n",ans);return 0; }

廣義后綴自動(dòng)機(jī):

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e4+100;char s[N];int tot=1,last=1,id[N<<1],tong[N<<1],cnt[N<<1][2];struct Node {int ch[26];int fa,len; }st[N<<1];void add(int x) {int p=last;//if(st[p].ch[x]){int q=st[p].ch[x];if(st[q].len==st[p].len+1)last=q;else{int np=last=++tot;st[np].len=st[p].len+1;st[np].fa=st[q].fa;st[q].fa=np;for(int i=0;i<26;i++)st[np].ch[i]=st[q].ch[i];while(st[p].ch[x]==q)st[p].ch[x]=np,p=st[p].fa;}return;}//int np=last=++tot;st[np].len=st[p].len+1;while(p&&!st[p].ch[x])st[p].ch[x]=np,p=st[p].fa;if(!p)st[np].fa=1;else{int q=st[p].ch[x];if(st[p].len+1==st[q].len)st[np].fa=q;else{int nq=++tot;st[nq]=st[q]; st[nq].len=st[p].len+1;st[q].fa=st[np].fa=nq;while(p&&st[p].ch[x]==q)st[p].ch[x]=nq,p=st[p].fa;//向上把所有q都替換成nq}} }void radix_sort() {memset(tong,0,sizeof(tong));for(int i=1;i<=tot;i++)tong[st[i].len]++;for(int i=1;i<=tot;i++)tong[i]+=tong[i-1];for(int i=1;i<=tot;i++)id[tong[st[i].len]--]=i; }int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);scanf("%s",s);for(int i=0;s[i];i++){add(s[i]-'a');cnt[last][0]++;}last=1;scanf("%s",s);for(int i=0;s[i];i++){add(s[i]-'a');cnt[last][1]++;}radix_sort();int ans=inf;for(int i=tot;i>1;i--){int cur=id[i],fa=st[cur].fa;if(cnt[cur][0]==1&&cnt[cur][1]==1)ans=min(ans,st[fa].len+1);cnt[fa][0]+=cnt[cur][0];cnt[fa][1]+=cnt[cur][1];}if(ans==inf)ans=-1;printf("%d\n",ans);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的CodeForces - 427D Match Catch(后缀数组/广义后缀自动机)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。