[HDU3336]Count the string(KMP+DP)
生活随笔
收集整理的這篇文章主要介紹了
[HDU3336]Count the string(KMP+DP)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Solution
不穩(wěn)定的傳送門
對KMP的靈活應(yīng)用
設(shè)dp[i]表示前[1,i]的答案
那么dp[i]=dp[p[i]]+1,p[i]為失配函數(shù)
Code
#include <cstdio> #include <algorithm> #include <cstring> using namespace std;const int mo=10007; int n,T,dp[200010],p[200010],Ans; char s[200010]; int main(){scanf("%d",&T);while(T--){scanf("%d\n%s",&n,s+1);Ans=0;memset(p,0,sizeof(p));memset(dp,0,sizeof(dp));for(int i=2,j=0;i<=n;++i){while(j>0&&s[i]!=s[j+1]) j=p[j];if(s[i]==s[j+1]) ++j;p[i]=j;}for(int i=1;i<=n;++i) Ans=(Ans+(dp[i]=dp[p[i]]+1))%mo;printf("%d\n",Ans);}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/void-f/p/8893624.html
總結(jié)
以上是生活随笔為你收集整理的[HDU3336]Count the string(KMP+DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OJ模板库
- 下一篇: Git 2.17改进了移动代码差异比较和