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 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]表示以str[i]结尾的字符串含有的前缀数量.

dp[i] = dp[next[i]] + 1即长度小于i的前缀数量+长度为i的前缀(1)。

#include <stdio.h>
#define maxn 200000 + 5
char str[maxn];
int next[maxn], len, dp[maxn];

void getNext(){
	int i = 0, j = -1;
	next[0] = -1;
	while(i < len){
		if(j == -1 || str[i] == str[j]){
			++i; ++j;
			next[i] = j;
		}else j = next[j];
	}
}

int main(){
	int t, sum;
	scanf("%d", &t);
	while(t--){
		scanf("%d%s", &len, str);
		getNext();
		sum = 0;
		for(int i = 1; i <= len; ++i){
			dp[i] = dp[next[i]] + 1;
			sum += dp[i] % 10007;
		}
		printf("%d\n", sum % 10007);
	}
	return 0;
}

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

时间: 2024-10-26 05:57:54

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】的相关文章

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

HDUOJ------3336 Count the string(kmp)

D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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 wr

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 Count the string (KMP)

题面见http://acm.hdu.edu.cn/showproblem.php?pid=3336 给你一个字符串,让你找它的前缀在整个字符串出现的次数. 作为一个不会思考的笨比,直接用kmp去一个个计数,果不其然,t了 找了博客来看,大概就是kmp+dp,要用到kmp中的pret数组(有的人习惯叫next数组,知道就行) dp的方程形式很简单,但很难理解. 这是原博主的原话: 如果用dp[i]表示该字符串前i个字符中出现任意以第i个字符结尾的前缀的次数,它的递推式是 dp[i]=dp[pret

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[]){//

Count the string kmp

问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个前缀,我们可以计算它在s中匹配的次数,因此我们可以看到前缀“a”匹配两次,“ab”也匹配两次,“ab a”匹配一次,“ab ab”匹配一次.现在,您需要计算所有前缀的匹配时间之和.对于“abab”,它是2+2+1+1=6.答案可能非常大,因此输出答案mod 10007. 输入第一行是一个整数t,表示

hud 3336 count the string (KMP)

这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了-- 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(abab)=num(a)+num(ab)+num(aba)+num(abab)=2+2+1+1=6; 题解:next[i]记录的是 长度为i 不为自身的最大首尾重复子串长度  num[i]记录长度为next[i]的前缀所重复出现的次数 附上代码: const mo=10007; var sum,next:

[HDU3336]Count the string(KMP+DP)

Solution 不稳定的传送门 对KMP的灵活应用 设dp[i]表示前[1,i]的答案 那么dp[i]=dp[p[i]]+1,p[i]为失配函数 Code #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int mo=10007; int n,T,dp[200010],p[200010],Ans; char s[200010]; int main(

[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