HDU 3336(Count the string)DP+KMP
来源:互联网 发布:快时尚销售数据报告 编辑:程序博客网 时间:2024/05/16 05:42
题目要求字符串所有前缀在字符串出现的次数之和,次数包括重叠部分(例如ababa,前缀aba出现次数为2次)
朴素思想,枚举前缀,各自KMP算个数,题目数据量较大,不允许这样。
前后缀问题一般会想到利用NEXT数组的性质。
例如abababa,NEXT:0012345,设dp[i]为第i字符为止,前缀(1~i)出现的次数,
dp[i]=dp[nextp[i]]+1代码
#include<iostream>#include<cstring>using namespace std;#define MOD 10007int nextp[200005],dp[200005];char s[200005];void Getnext(int len){ int i,j; i=0,j=-1; nextp[0]=-1; while(i<len) { if(j==-1 || s[i]==s[j]) { i++; j++; nextp[i]=j; } else j=nextp[j]; }}int main(){ int T; int i,j,k,ans,n; cin>>T; while(T--) { cin>>n; cin>>s; Getnext(n); ans=0; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { dp[i]=(dp[nextp[i]]+1)%MOD; ans=(ans+dp[i])%MOD; } cout<<ans<<endl; } return 0;}
0 0
- HDU 3336 Count the string(KMP+dp)
- [HDU 3336]Count the String[kmp][DP]
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string KMP+DP
- HDU 3336 Count the string kmp+dp
- hdu -- 3336 Count the string(KMP + dp)
- hdu 3336 Count the string 【kmp + dp】
- hdu 3336 Count the string KMP+DP
- HDU 3336(Count the string)DP+KMP
- HDU 3336 Count the string (KMP + DP)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 Count the string (kmp + dp)
- Hdu-3336 Count the string(KMP + DP)
- HDU 3336 Count the string 【KMP】【dp】
- HDU 3336 Count the string (kmp+dp)
- HDU 3336 Count the string(经典,KMP+DP)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 Count the string (DP+kmp)
- 无线网络覆盖
- Nginx开发从入门到精通
- 设计模式-工厂模式
- 数组中出现次数超过一半的数字
- CollapsingAvatarToolbar 头像随ListView滚动缩回到ActionBar特效
- HDU 3336(Count the string)DP+KMP
- [android] ViewPager has not been bound using JakeWharton/ViewPagerIndicator
- 树根Digital root算法
- 设计模式-单例模式
- 理想
- ID3和C4.5的区别
- Android ORM系列之GreenDao最佳实践
- servlet 和JSP
- jetty部署异常:rg.apache.jasper.JasperException: PWC6345: There is an error in invoking ja