hdu 3336 Count the string(next数组)

题意:统计前缀在串中出现的次数

思路:next数组,递推

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

#define MaxSize 200005
#define Mod 10007

char str[MaxSize];
int _next[MaxSize];
int dp[MaxSize];
int len;

void GetNext(char t[]){//求next数组
    int j,k;//,len;
    j=0;
    k=-1;
    _next[0]=-1;
    //len=strlen(t);
    while(j<len){
        if(k==-1||t[j]==t[k]){
            ++j;
            ++k;
            _next[j]=k;//此句可由优化替代
            /*优化(仅保证求KMPIndex时可用。谨慎使用。)
            if(t[j]!=t[k])next[j]=k;
            else next[j]=next[k];
            */
        }
        else k=_next[k];
    }
}

int main(){
    int t,i,ans;
    scanf("%d",&t);
    while(t--){
        scanf("%d%s",&len,str);
        GetNext(str);//求子串的next数组
        dp[0]=0;
        ans=0;
        for(i=1;i<=len;++i){
            dp[i]=dp[_next[i]]+1;
            dp[i]%=Mod;
            ans+=dp[i];
            ans%=Mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-10-21 07:58:13

hdu 3336 Count the string(next数组)的相关文章

HDU 3336 Count the string (next数组活用)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5224    Accepted Submission(s): 2454 Problem Description It is well known that AekdyCoin is good at string problems as well as n

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

hdu 3336 Count the string

Count the stringTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4239    Accepted Submission(s): 1977 Problem Description It is well known that AekdyCoin is good at string problems as well as num

HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

KMP——hdu 3336 count the string

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10478    Accepted Submission(s): 4893 Problem Description It is well known that AekdyCoin is good at string problems as well as n

hdu 3336 Count the string KMP+DP

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as n

HDU 3336 Count the string(KMP算法next数组的应用)

解题思路: 求前缀数组出现的次数之和,next[i] > 0 表示长度为next[i]的前缀又出现了一次. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #

HDU 3336 Count the string (基础KMP)

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: "abab" The prefixes are: "a", "ab&qu

hdu 3336 Count the string【kmp】

http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,问字符串每一个前缀在字符串中的出现总次数. 思路:kmp的应用,自身和自身进行匹配,每次匹配时,如果没有匹配到结束,模式串按next数组向后移动,出现匹配至结束的情况,匹配串往后移动一位 ,匹配过程中,出现失配时,统计当前已经匹配的字符个数,累加到总数中,直到匹配串移动到最后一位. 其实前两天就在网上看题解用kmp+dp的方法来写了这道题,然而后来细思一些细节,自己并没有弄懂,所以