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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

牛客 - Connie(AC自动机+dp/KMP+dp)

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

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

題目大意:給出一個(gè)匹配串 sss,現(xiàn)在問(wèn)模式串 ttt 的期望得分。其中假設(shè)匹配串在模式串中的出現(xiàn)次數(shù)為 xxx,那么將得到 2x2^x2x 的分?jǐn)?shù)

題目分析:涉及到了期望一開(kāi)始還以為是概率,后來(lái)發(fā)現(xiàn)其實(shí)就是個(gè)計(jì)數(shù)問(wèn)題

題目實(shí)際讓我們操作的就是,枚舉 5n5^n5n 種字符串,然后對(duì)于每種字符串統(tǒng)計(jì)匹配串 sss 的出現(xiàn)次數(shù),注意這里的出現(xiàn)次數(shù)并不能說(shuō)是最多或最少的出現(xiàn)次數(shù),先將貢獻(xiàn)的式子轉(zhuǎn)換一下:
2x=(...((1)?2)?2)?2...)?22^x=(...((1)*2)*2)*2...)*22x=(...((1)?2)?2)?2...)?2

假設(shè)枚舉的字符串 ttt 中可以匹配一次 sss ,那么只需要將匹配次數(shù)相應(yīng)的乘以二就可以實(shí)現(xiàn)上面的公式了,關(guān)于期望的話最后只需要除以 5n5^n5n 就好啦

現(xiàn)在問(wèn)題轉(zhuǎn)換為了字符串計(jì)數(shù)問(wèn)題,我的第一反應(yīng)就是 ACACAC 自動(dòng)機(jī)裸題啊,直接掛上模板過(guò)了,簡(jiǎn)單講一下思路,就是將匹配串扔進(jìn) ACACAC 自動(dòng)機(jī)里,dpi,jdp_{i,j}dpi,j? 代表到了字符串 ttt 的第 iii 個(gè)位置時(shí),在自動(dòng)機(jī)里匹配的狀態(tài)為 jjj 的方案數(shù),轉(zhuǎn)移的話就是枚舉第 i+1i+1i+1 的字符記為 chchch,方程就是:
dpi+1,trie[j][ch]+=dp[i][j]dp_{i+1,trie[j][ch]}+=dp[i][j]dpi+1,trie[j][ch]?+=dp[i][j]

非常簡(jiǎn)單的過(guò)了,不過(guò)后續(xù)意識(shí)到是不是有點(diǎn)大材小用了?因?yàn)檫@個(gè)題目只有一個(gè)匹配串啊,為什么不直接用 KMPKMPKMP 去寫(xiě)呢?然后就用 KMPKMPKMP 也寫(xiě)了一發(fā)。。不過(guò)有一說(shuō)一,對(duì)于失配狀態(tài)轉(zhuǎn)移的 dpdpdp,以后還是用自動(dòng)機(jī)來(lái)寫(xiě)吧,因?yàn)橄啾戎?KMPKMPKMP 的細(xì)節(jié)略多

代碼:
AC自動(dòng)機(jī)

// Problem: Connie // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/17148/D // Memory Limit: 1048576 MB // Time Limit: 2000 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=110; const int mod=998244353; const char str[]="conie"; char s[N]; LL dp[N][N]; int fail[N],flag[N],trie[N][26],cnt; int newnode() {cnt++;for(int i=0;i<26;i++) {trie[cnt][i]=0;}flag[cnt]=false;return cnt; } void insert_word() {int len=strlen(s);int pos=0;for(int i=0;i<len;i++){int to=s[i]-'a';if(!trie[pos][to])trie[pos][to]=newnode();pos=trie[pos][to];}flag[pos]=true; } void getfail() {queue<int>q;for(int i=0;i<26;i++){if(trie[0][i]){fail[trie[0][i]]=0;q.push(trie[0][i]);}}while(!q.empty()){int cur=q.front();q.pop();for(int i=0;i<26;i++){if(trie[cur][i]){fail[trie[cur][i]]=trie[fail[cur]][i];q.push(trie[cur][i]);}elsetrie[cur][i]=trie[fail[cur]][i];}} } LL q_pow(LL a,LL b) {LL ans=1;while(b) {if(b&1) {ans=ans*a%mod;}a=a*a%mod;b>>=1;}return ans; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;read(n),read(m);scanf("%s",s);insert_word();getfail();dp[0][0]=1;for(int i=0;i<n;i++) {for(int j=0;j<=cnt;j++) {for(int k=0;k<5;k++) {int nj=trie[j][str[k]-'a'];dp[i+1][nj]=(dp[i+1][nj]+dp[i][j]*(flag[nj]?2:1))%mod;}}}LL ans=0;for(int i=0;i<=cnt;i++) {ans=(ans+dp[n][i])%mod;}ans=(ans*q_pow(q_pow(5,n),mod-2))%mod;cout<<ans<<endl;return 0; }

KMP:

// Problem: Connie // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/17148/D // Memory Limit: 1048576 MB // Time Limit: 2000 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=1e6+100; const int mod=998244353; const char str[]="conie"; char s[N]; int nt[N]; void get_next() {int len=strlen(s+1);nt[1]=0;for(int i=2,j=0;i<=len;i++) {while(j!=0&&s[j+1]!=s[i]) {j=nt[j];}if(s[j+1]==s[i]) {j++;}nt[i]=j;} } LL dp[110][110]; LL q_pow(LL a,LL b) {LL ans=1;while(b) {if(b&1) {ans=ans*a%mod;}a=a*a%mod;b>>=1;}return ans; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;read(n),read(m);scanf("%s",s+1);get_next();dp[0][0]=1;for(int i=0;i<n;i++) {for(int j=0;j<=m;j++) {for(int k=0;k<5;k++) {char ch=str[k];int p=j;while(p!=0&&ch!=s[p+1]) {p=nt[p];}if(ch==s[p+1]) {p++;}dp[i+1][p]=(dp[i+1][p]+dp[i][j]*(p==m?2:1))%mod;}}}LL ans=0;for(int i=0;i<=m;i++) {ans=(ans+dp[n][i])%mod;}ans=(ans*q_pow(q_pow(5,n),mod-2))%mod;cout<<ans<<endl;return 0; }

總結(jié)

以上是生活随笔為你收集整理的牛客 - Connie(AC自动机+dp/KMP+dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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