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

题意:求一个串中所有的前缀各自在串中出现次数的和。

分析:先来看看一个例子

i      1  2  3  4  5  6

str[i]   a  b  a  b  a  b

next[i] 0  0  1  2  3  4

next[i]=j; 表示从1----j 和 i-j+1----i这一段是相同的,利用next数组,依次推导

例如: i=5时 ababa  所包含的 前缀数 等于 以第3个a结尾的前缀数(即ababa本身) + (next[5]=3)str[3]所包含的前缀数量(即aba包含的前缀数量)

于是可以得到递推方程 dp[j]=dp[next[j]]+1  dp[0]=0;

对于这个例子,可以有:

dp[1]=dp[n[1]]+1=1;  a

dp[2]=dp[n[2]]+1=1;  ab

dp[3]=dp[n[3]]+1=2; a aba

dp[4]=dp[n[4]]+1=2; ab abab

dp[5]=dp[n[5]]+1=3; a aba ababa

dp[6]=dp[n[6]]+1=3; ab abab ababab

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
char s[222222];
int n;
int nt[222222];
int dp[222222];

void getnext()
{
    int k=-1,j=0;
    nt[0]=-1;

    while(j<n)
    {
        if(k<0 || s[j]==s[k])
        {
            j++;
            k++;
            nt[j]=k;
        }
        else
            k=nt[k];
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        scanf("%s",s);

        getnext();

//        for(int i=0;i<=n;i++)
//            cout<<nt[i]<<" ";
//        cout<<endl;
     for(int i=1;i<=n;i++) dp[i]=1;
     dp[0]=0;

     int ans=0;
     for(int i=1;i<=n;i++)
     {
         dp[i]=dp[nt[i]]+1;
         ans=(ans+dp[i])%10007;
     }
     printf("%d\n",ans);

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 16:43:04

hdu 3336 Count the string KMP+DP的相关文章

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(DP+KMP)(好题)

题意:对于长度为len的字符串,我们知道它包含有len个前缀,现在要你统计出这个字符串里面,包含这些前缀的总个数. 思路:这题可以运用KMP的next数组来解,不过也太难想了吧orz,为了用next解这题想那么多也不算是很好的方法orz. 如何根据next数组的性质来解这道题,next数组的值是当前子串的后缀与前缀匹配的个数,所以根据这个性质把题待求的对象改一下:求每种字母作为结尾的串在原串中出现的次数.从最后一个字母分析,首先字符串本身就是以last字母作结,next[last]=x,所以又找

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

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】

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

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

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