Count the string---hdu3336(kmp Next数组的运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336

题意就是求串s的前缀的个数和;

例如:abab

前缀 个数

a     2

ab    2

aba   1

abab  1

总数:6

dp[i] 表示前面的字符以s[i-1]结尾的前缀个数;上列中dp[4]=2(以最后一个字符b结尾的前缀) {abab,ab};

可以看出增加一个字母会产生一个新的前缀,那就是整个串,之前的前缀就是Next[i]的位置所对应的dp,即dp[Next[i]];

所以dp[i] = dp[Next[i]] + 1;-_-主要是仔细的想一下,有点绕;

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

const int N = 2e6+7;
const int mod = 10007;

char s[N];
int dp[N], n, Next[N];

void GetNext()
{
    int i=0,j=-1;
    Next[0] = -1;
    while(i<n)
    {
        if(j==-1 || s[i]==s[j])
            Next[++i] = ++j;
        else
            j = Next[j];
    }
}
int main()
{
    int T, sum;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        scanf("%s", s);
        GetNext();
        memset(dp, 0, sizeof(dp));
        sum = 0;
        for(int i=1; i<=n; i++)
        {
            dp[i] = dp[Next[i]] + 1;
            sum = (sum + dp[i]) % mod;
        }
        printf("%d\n", sum);
    }
    return 0;
}

时间: 2024-10-24 23:24:48

Count the string---hdu3336(kmp Next数组的运用)的相关文章

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】

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

hdu3336(Count the string)KMP的应用

题意:给一个字符串,计算所有前缀在字符串中出现的次数和. 解法:KMP计算出Next数组后,每个位置的Next数组不断往前递归,每次相应前缀次数就加1. 代码: /****************************************************** * author:xiefubao *******************************************************/ #pragma comment(linker, "/STACK:102400

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

Count the string(hdu3336)

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

喝豆浆 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

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

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)

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+dp)

题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可. #include <map> #include <set> #include <list> #include <cmath> #include <queue>

[hdu3336]kmp(后缀数组)

题意:求字符串s的所有前缀出现次数之和. http://www.cnblogs.com/jklongint/p/4446117.html 思路:用kmp做,简单且效率高.以前缀结尾的位置分类,令dp[i]为以结尾位置在i的前缀数量,那么dp[i] = cnt(j)(j~i是前缀),而由kmp的next函数的转移性质,可得如下递推方程:dp[i] = dp[next[i]] + 1,把这个递推式不断展开,也就是i = next[i]不断迭代,那么+1的个数就是dp[i] = cnt(j)(j~i是