(KMP)Count the string -- hdu -- 3336

http://acm.hdu.edu.cn/showproblem.php?pid=3336

Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6672    Accepted Submission(s): 3089

Problem Description

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", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.

Input

The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.

Output

For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.

Sample Input

1

4

abab

Sample Output

6

dp[i] = dp[Next[i]] + 1;

但是dp[Next[i]] = dp[dp[Next[i]]+1]+1;

感觉有点像递归的思想,dp[i] 加 1 是算它本身,然而dp[Next[i]]代表的是长度为 i 的最大匹配(前缀和后缀的最大匹配)在母串中的个数,然后层层找出所有的个数

例:

  1 2 3 4 5
  a b a b a
-1 0 0 1 2 3

为什么要用Next呢,因为要看后缀

dp[1] 代表的是长度为1的串中分别有1个 a

dp[2] 代表的是长度为2的串中分别有1个 ab

dp[3] 代表的是长度为3的串中分别有1个 aba, a

dp[4] 代表的是长度为4的串中分别有1个 abab, ab

dp[5] 代表的是长度为5的串中分别有1个 ababa, aba, a

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

#define MOD 10007
#define N 210000

char M[N];
int Next[N], dp[N];

void FindNext(int len)
{
    int i=0, j=-1;
    Next[0] = -1;

    while(i<len)
    {
        if(j==-1 || M[i]==M[j])
            Next[++i] = ++j;
        else
            j = Next[j];
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, i, Sum=0;
        memset(dp, 0, sizeof(dp));

        scanf("%d%s", &n, M);

        FindNext(n);
        for(i=1; i<=n; i++)
        {
            dp[i] = (dp[Next[i]] + 1)%MOD;
            Sum = (dp[i]+Sum)%MOD;
        }

        printf("%d\n", Sum);
    }
    return 0;
}

时间: 2024-10-13 16:18:45

(KMP)Count the string -- hdu -- 3336的相关文章

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; ManacherK - Count the string HDU - 3336(前缀数量问题)

K - Count the string HDU - 3336 题目链接:https://vjudge.net/contest/70325#problem/K 题目: 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 t

Count the string HDU - 3336

题意: 求一个字符串的每个前缀在这个字符串中出现次数的加和 解析: 默默的骂一句...傻xkmp..博主心里气愤... 拓展kmp就好多了... 因为拓展kmp每匹配一次   就相当于这些前缀出现了一次 如abcabc abcabc  与 abcabc匹配 为6  这个6就相当于 abcabc  abcab  abca abc ab a各出现了一次 abcabc  与 bcabc 匹配 0 ···· abcabc 与 abc 匹配 为3 相当于abc ab  a 各出现了一次 明白了吧.. km

Count the string - HDU 3336(next+dp)

题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", "abab", "ababa"}: 每个前缀串出现的次数{3, 2, 2, 1, 1},那么结果就是 9.   分析:我们可以用dp[i],表示前i长度的串的结果,那么就可以得到下面的转移方程 dp[i] = dp[next[i]] + 1. 代码如下. ===========

C语言记忆化搜索___Count the string(Hdu 3336)

Problem Description 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: "

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

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 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

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

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