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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU - 4641 K-string(后缀自动机)

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

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

題目大意:給出一個(gè)字符串 s ,規(guī)定 K-string 為字符串 s 中出現(xiàn)次數(shù)大于等于 k 次的字串,現(xiàn)在有 m 次操作,每次操作有兩種:

  • 1 ch:在字符串 s 后面添加字符 ch
  • 2:詢問當(dāng)前的字符串中有多少個(gè)子串為 K-string
  • 題目分析:因?yàn)楹缶Y自動(dòng)機(jī)是在線構(gòu)造的,所以操作一我們可以直接用后綴自動(dòng)機(jī)來完成,而操作二我們可以直接在后綴鏈接上不斷向上維護(hù)即可,因?yàn)閿?shù)據(jù)比較水,所以這樣暴力的更新是可以過的,不過需要注意的是還是要求了對(duì)后綴自動(dòng)機(jī)的理解以及對(duì)代碼的改寫能力有一定要求,還是得加深對(duì)SAM的理解啊

    代碼:

    #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=3e5+100;char s[N];int tot,last,n,k,m,cnt[N<<1];LL ans;struct Node {int ch[26];int fa,len; }st[N<<1];void newnode() {tot++;for(int i=0;i<26;i++)st[tot].ch[i]=0;st[tot].fa=st[tot].len=0;cnt[tot]=0; }void add(int x) {newnode();int p=last,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;cnt[nq]=cnt[q];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}}int pos=last;while(pos&&cnt[pos]<k){cnt[pos]++;if(cnt[pos]>=k)ans+=st[pos].len-st[st[pos].fa].len;pos=st[pos].fa;} }void init() {ans=tot=0,last=1;newnode();scanf("%s",s);for(int i=0;i<n;i++)add(s[i]-'a'); }int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);while(scanf("%d%d%d",&n,&m,&k)!=EOF){init();while(m--){int op;scanf("%d",&op);if(op==1){scanf("%s",s);add(s[0]-'a');}elseprintf("%lld\n",ans);}}return 0; }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的HDU - 4641 K-string(后缀自动机)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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