CodeForces - 1337E Kaavi and Magic Spell(dp)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1337E Kaavi and Magic Spell(dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 n 的字符串 s 和一個長度為 m 的字符串 t? ( n >= m ),現在有一個初始為空的字符串 a ,可以進行最多 n 次操作,每次操作可以二選一:
問可以構造出的 2^n 種情況里,有多少個字符串 a 是滿足前綴包含字符串 t 的
題目分析:因為答案需要取模,一般不是取模就是組合數學,而這個題目實質上是一個最優性問題,那么就是 dp 跑不了了
因為 n 比較小,支持?n * n 的操作,以及可以開到二維 dp
就只能分析到這了。。因為我的dp不太好,所以接下來直接說題解吧
初始時我們將字符串 t 的長度與字符串 s 的長度視為相同,dp[ i ][ j ] 代表字符串 t 的區間 [ i , j ] 所代表的的子串的可達方案數,那么答案顯然就是 dp[ 1 ][ m ] + dp[ 1 ][ m + 1 ] + ... + dp[ 1 ][ n ] 了
關于初始化,分兩種情況討論:
關于狀態轉移,也是分兩種情況討論,遍歷字符串 s ,枚舉 l 和 r 進行轉移:
轉移方程應該不難理解吧,如果需要比較的位置越界了,或者相等的話才進行轉移,然后稍微注意一下枚舉的順序,就能做到 n?* n 轉移狀態了
代碼:
#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=3e3+100;const int mod=998244353;LL dp[N][N];char s[N],t[N];int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);scanf("%s%s",s+1,t+1);int n=strlen(s+1),m=strlen(t+1);for(int i=1;i<=m;i++)if(t[i]==s[1])dp[i][i]=2;for(int i=m+1;i<=n;i++)dp[i][i]=2;for(int len=2;len<=n;len++){char ch=s[len];for(int l=1;l+len-1<=n;l++){int r=l+len-1;if(l>m||ch==t[l])//加在前面 dp[l][r]=(dp[l][r]+dp[l+1][r])%mod;if(r>m||ch==t[r])//加在后面 dp[l][r]=(dp[l][r]+dp[l][r-1])%mod;}}LL ans=0;for(int i=m;i<=n;i++)ans=(ans+dp[1][i])%mod;printf("%lld\n",ans);return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1337E Kaavi and Magic Spell(dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1335F R
- 下一篇: CodeForces - 1337D X