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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Codeforces 235C

發布時間:2023/12/3 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 235C 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Codeforces 235C


題目:給定一主串\(S\)\(n\)次詢問,每次詢問串\(t\)的所有循環移位串的出現的次數和

做法:建\(SAM\),對于詢問串\(t\),將他復制一份放在后邊,在后綴自動機上匹配,如果匹配長度大于\(|t|\),就沿著\(fa\), 找到第一次大于\(|t|\)的位置,用這個狀態的\(right\)數組更新答案。注意到可能會匹配到重復的狀態,所以要對會更新答案的狀態去重。

#include <bits/stdc++.h> typedef long long ll; const int N = 1000010 << 1; using namespace std;struct SAM{int n, step[N], fa[N], ch[N][26], right[N], num[N], last, root, cnt;char s[N>>1];SAM(){last = root = ++cnt; }void add(int x){int tmp = s[x] - 'a', p = last, np = ++cnt;step[last = np] = x, right[np] = 1;while(p && !ch[p][tmp]) ch[p][tmp] = np, p = fa[p];if(!p) fa[np] = root;else{int q = ch[p][tmp];if(step[q] == step[p] + 1) fa[np] = q;else{int nq = ++cnt; step[nq] = step[p] + 1;memcpy(ch[nq], ch[q], sizeof(ch[q]));fa[nq] = fa[q], fa[q] = fa[np] = nq;while(ch[p][tmp] == q) ch[p][tmp] = nq, p = fa[p];}}}int tmp[N];void calright(){memset(tmp, 0, sizeof(tmp));for(int i = 1; i <= cnt; i++) tmp[step[i]]++;for(int i = 1; i <= n; i++) tmp[i] += tmp[i - 1];for(int i = cnt; i; i--) num[tmp[step[i]]--] = i;for(int i = cnt; i; i--) right[fa[num[i]]] += right[num[i]];}void run(){scanf(" %s",s+1), n = strlen(s + 1);for(int i = 1; i <= n; i++) add(i);calright();}char str[N];int len;set<int> S;ll solve() {scanf(" %s",str+1), len = strlen(str+1);for(int i = 1; i <= len; ++i) str[i+len] = str[i];int now = root, tmp = 0; ll ans = 0;for(int i = 1; i <= len+len; ++i) {int x = str[i]-'a';if(ch[now][x]) now = ch[now][x], ++tmp;else {while(now && !ch[now][x]) now = fa[now];if(!now) now = root, tmp = 0;else tmp = step[now]+1, now = ch[now][x];}while(now!=1 && step[fa[now]] >= len) now = fa[now], tmp = step[now];if(tmp >= len) S.insert(now);}for(set<int>::iterator it = S.begin(); it != S.end(); ++it) ans += right[*it];S.clear();return ans;} } Fe;int main() {Fe.run();int q;scanf("%d",&q);while(q--) {printf("%I64d\n",Fe.solve());} }

轉載于:https://www.cnblogs.com/RRRR-wys/p/10299830.html

總結

以上是生活随笔為你收集整理的Codeforces 235C的全部內容,希望文章能夠幫你解決所遇到的問題。

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