hdu 3336题 +kmp模板

大意 :
找出n个前缀出现了多少次
求出next函数  每个点有dp[i]=dp[next[i]]+1次;
ac代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200010;
const int mod = 10007;
char str[maxn];
int len;
int dp[maxn],next1[maxn];
void getnext(){//模版部分
    int i=0,j=-1;
    next1[0]=-1;
    while(i<len){
        if(j==-1||str[i]==str[j]) next1[++i]=++j;
        else j=next1[j];
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d %s",&len,str);
        getnext();
        memset(dp,0,sizeof(dp));
        int ans=0;
        for(int i=1;i<=len;i++){//用strlen会超时
            dp[i]=dp[next1[i]]+1;
            ans=(ans+dp[i])%mod;
        }
        printf("%d\n",ans);
    }
}

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务