码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3336 count the string(KMP+dp)

时间:2015-09-03 23:15:34      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

题意:

求给定字符串,包含的其前缀的数量。

分析:

就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 200010
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  10007;
int dp[N],f[N],n;
char str[N];
void getnext(){
    int i=0,j=-1;
    f[0]=-1;
    while(i<n){
        if(j==-1||str[i]==str[j])
        {
            i++;j++;
            f[i]=j;
        }
        else j=f[j];
    }
}
void solve(){
    getnext();
        //cout<<1<<endl;
    memset(dp,0,sizeof(dp));
    int total=0;
    for(int i=1;i<=n;++i){
        dp[i]=(dp[f[i]]+1)%mod;
        total=(total+dp[i])%mod;
    }
    printf("%d\n",total);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf("%s",str);
        solve();
    }
return 0;
}

 

hdu 3336 count the string(KMP+dp)

标签:

原文地址:http://www.cnblogs.com/zsf123/p/4780766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!