hdu3336 Count the string(kmp+dp)
来源:互联网 发布:windows 多进程 编辑:程序博客网 时间:2024/05/16 11:39
题意:给出一个字符串,求所有前缀串出现的总次数。
abab 的前缀串有:a ab aba abab
其中a出现2次
总共出现6次。
思路:先是用KMP求出第i个字符重复的最长前缀的结束位置
dp[i]代表长为i的前缀串(数量1)与其有重复前缀数量。
#include<iostream>#include<queue>using namespace std;const int N = 222222,M = 10007;int dp[N],next[N];char s[N];void get_next(int n){for (int i=2,j=0;i<=n;i++){//cout<<s[j+1]<<s[i]<<endl;while (j && s[j+1]!=s[i]) j=next[j];if (s[j+1] == s[i]) j++;next[i]=j;}}int main(){freopen("in","r",stdin);int t,n;cin>>t;while (t--){scanf("%d",&n);scanf("%s",s+1);s[0]='#';get_next(n);for (int i=1;i<=n;i++) dp[i]=1;int ans=0;//for (int i=1;i<=n;i++) cout<<next[i]<<' ';cout<<endl;for (int i=1;i<=n;i++){dp[i]=dp[next[i]]+1;ans+=dp[i];ans%=M;}printf("%d\n",ans);}}
0 0
- hdu3336 Count the string KMP+dp
- hdu3336 Count the string(kmp+dp)
- hdu3336 Count the string--KMP+DP
- [dp][KMP][hdu3336]Count the string
- hdu3336 Count the string(kmp+dp)
- 【KMP】 hdu3336 Count the string
- hdu3336 Count the string-------KMP
- hdu3336 Count the string KMP
- [KMP][HDU3336][Count the string]
- HDU3336 Count the string (KMP)
- hdu3336—Count the string(kmp+dp)
- HDU3336-Count the string(KMP)
- hdu3336 Count the string(KMP应用)
- HDU3336 Count the string(KMP)
- HDU3336 Count the string KMP+递推
- hdu3336 Count the string (KMP)
- hdu3336(kmp)Count the string
- hdu3336 Count the string(dp)
- UVA 1345 - Jamie's Contact Groups(二分+最大流)
- [Cocoa]_[初级]_[NSTimer在Cocoa中的应用,主要以多张图片切换的播放和停止]
- Java Socket编程
- jvm虚拟机特性之内存区域理解
- mongodb主从复制
- hdu3336 Count the string(kmp+dp)
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
- hdu-5015(矩阵快速幂)
- ACM steps 1.1.4
- 从 C++ 单例 pthread_once 到 double-check、volatile、memory barrier
- 0 - 如何从Silverlight和WPF的XAML迁移至Windows Store Apps
- 冒泡排序理解
- redis数据结构使用场景
- C++基础之访问const变量、访问私有变量and联合体(狗仔队)