K - Count the string

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.

InputThe 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. 
OutputFor 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

一个结论:cnt[i] 表示 因为加入i字符而增加的 以i字符结束的前缀数目cnt[i] = cnt[Next[i]] + 1Next[]数组表示前j个字符中最长的相同前缀后缀长度证明:如果Next[i]==0 也就是说 i字符没有在之前出现过,那么因为加入字符i增加的前缀只有0-i这个字符串否则  增加的数目要考虑由于第i-k个字符到第i个字符组成的字符串是前缀的情况,增加的数目为这种前缀的数目+1,那么这种前缀的数目如何求?显然,考虑i之前最长匹配的前缀后缀,在最长匹配处加入字符i增加的前缀 等于 在i处加入字符i增加的前缀
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 200002
#define INF 0x3f3f3f3f
#define M 10007
typedef long long LL;
/*
输出字符串中所有前缀在字符串中出现的次数
ababab
*/
char t[MAXN];
int Next[MAXN],cnt[MAXN];
void kmp_pre(char t[])
{
    int m = strlen(t);
    int j,k;
    j = 0;k = Next[0] = -1;
    while(j<m)
    {
        if(k==-1||t[j]==t[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int l,ans=0;
        scanf("%d",&l);
        scanf("%s",t);
        kmp_pre(t);
        for(int i=1;i<=l;i++)
        {
            cnt[i] = (cnt[Next[i]]+1);
            ans = (ans+cnt[i])%M;
        }
        printf("%d\n",ans);
    }
}
时间: 2024-11-29 05:57:56

K - Count the string的相关文章

[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

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

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

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

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

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

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

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