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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CodeForces - 528D Fuzzy Search(多项式匹配字符串)

發布時間:2024/4/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 528D Fuzzy Search(多项式匹配字符串) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 nnn 的字符串 sss 和一個長度為 mmm 的字符串 ttt,問字符串 sss 有哪些子串可以匹配 ttt。給出一個參數 kkkTTTSSS 的第 iii 個位置中出現,當且僅當把 TTT 的首字符和 SSS 的第 iii 個字符對齊后,TTT 中的每一個字符能夠在 SSS 中找到一個位置偏差不超過 kkk 的相同字符。

需要注意的是,在字符串 sss 中通過偏差匹配的字符可以重復與 ttt 匹配

題目分析:預處理出數組 g[i][j]g[i][j]g[i][j],代表字符串 sss 的第 iii 個位置可以放置字符 jjj,然后就是套路了:枚舉每個字符,將字符串 ttt 反轉,用 NTTNTTNTT 處理出數組 f(x)f(x)f(x) 代表子串 s[x?m+1:x]s[x-m+1:x]s[x?m+1:x] 與字符串 ttt 可以匹配的位置,最后需要統計 f(i)=mf(i)=mf(i)=m 的個數

代碼:

// Problem: D. Fuzzy Search // Contest: Codeforces - Codeforces Round #296 (Div. 1) // URL: https://codeforces.com/contest/528/problem/D // Memory Limit: 256 MB // Time Limit: 3000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #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<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=2e5+100; const int mod=998244353,G=3,Gi=332748118; int n,m,k,limit=1,L,r[N<<2]; LL a[N<<2],b[N<<2],f[N<<2]; char s[N],t[N]; inline LL fastpow(LL a, LL k) {LL base = 1;while(k) {if(k & 1) base = (base * a ) % mod;a = (a * a) % mod;k >>= 1;}return base % mod; } inline void NTT(LL *A, int type) {for(int i = 0; i < limit; i++) if(i < r[i]) swap(A[i], A[r[i]]);for(int mid = 1; mid < limit; mid <<= 1) { LL Wn = fastpow( type == 1 ? G : Gi , (mod - 1) / (mid << 1));for(int j = 0; j < limit; j += (mid << 1)) {LL w = 1;for(int k = 0; k < mid; k++, w = (w * Wn) % mod) {int x = A[j + k], y = w * A[j + k + mid] % mod;A[j + k] = (x + y) % mod,A[j + k + mid] = (x - y + mod) % mod;}}} } void init() {limit=1,L=0;while(limit<=n+m) limit<<=1,L++;for(int i=0;i<limit;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(L-1)); } void solve(char ch) {for(int i=0;i<limit;i++) {a[i]=b[i]=0;}int last=-inf;for(int i=0;i<n;i++) {if(s[i]==ch) {last=i;}if(i-last<=k) {a[i]=1;}}last=inf;for(int i=n-1;i>=0;i--) {if(s[i]==ch) {last=i;}if(last-i<=k) {a[i]=1;}}for(int i=0;i<m;i++) {b[i]=t[i]==ch;}NTT(a,1),NTT(b,1);for(int i=0;i<limit;i++) {f[i]=(f[i]+a[i]*b[i])%mod;} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);read(n),read(m),read(k);init();scanf("%s%s",s,t);reverse(t,t+m);solve('A'),solve('C'),solve('G'),solve('T');NTT(f,-1);LL inv=fastpow(limit,mod-2);for(int i=0;i<=n+m;i++) {f[i]=(f[i]*inv)%mod;}int ans=0;for(int i=m-1;i<n;i++) {ans+=f[i]==m;}cout<<ans<<endl;return 0; }

總結

以上是生活随笔為你收集整理的CodeForces - 528D Fuzzy Search(多项式匹配字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。

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