HDU3336 KMP+DP

Count the string

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

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

Author

[email protected]

题意:

求长度为n的字符串中每个前缀在这条字符串中出现的次数的总和

代码:

//dp[i]表示字符串s[1~i]中出现了多少个以s[i]为结尾的前缀,又有f数组的特性,如果
//f[i]=j则s[1~j]=s[i-j+1~i]得出状态转移方程dp[i]=dp[f[i]]+1;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=10007;
char P[200005];
int f[200005],dp[200005];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf("%s",P);
        f[0]=f[1]=0;
        for(int i=1;i<n;i++){
            int j=f[i];
            while(j&&P[i]!=P[j]) j=f[j];
            f[i+1]=(P[i]==P[j]?j+1:0);
        }
        dp[0]=0;
        int sum=0;
        for(int i=1;i<=n;i++){
            dp[i]=dp[f[i]]+1;
            sum+=dp[i]%mod;
            sum%=mod;
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-10-22 01:55:53

HDU3336 KMP+DP的相关文章

codeforces432D Prefixes and Suffixes(kmp+dp)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j

【KMP+DP】Count the string

KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. 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 str

[HDOJ5763]Another Meaning(KMP, DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种替换方法. kmp求出b在a中完全匹配后的结尾位置,然后dp(i)表示匹配到i时替换的方案数(不替换也算一次方案).首先更新dp(i)=dp(i-1),当且仅当i点是一个完全匹配的终点时,加上dp(i-nb)处的值. 1 #include <bits/stdc++.h> 2 using names

[kmp+dp] hdu 4628 Pieces

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2096    Accepted Submission(s): 715 Problem Description Now you are back,and

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

HDU3336(KMP + dp)

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

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 3689 Infinite monkey theorem [KMP DP]

Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1702 Accepted Submission(s): 882 Problem Description Could you imaging a monkey writing computer programs? Surely monkeys are

Codeforces 432D Prefixes and Suffixes(KMP+dp)

题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出全部的前后缀串,然后利用dp求解,dp[i]表示从1到i这个子串出现过的次数.转移方程dp[jump[i]]+=dp[i].随意一个dp[i]的初始状态应该是1. #include <cstdio> #include <cstring> const int N = 1e5+5; int