hdu3336 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", "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题意:找每个前缀在字符串中的出现次数题解:kmp的next数组处理,刚开始从后往前遍历,每个next数值往前递归加上次数,最后就是结果,交了之后发现tle(没有发现最差的时间复杂度居然有O(n*n)),于是乎只能改进一下,用num数组来存次数,每次递归的时候就把当前num++,这样只需要遍历一遍就行了时间复杂度变成了O(n)果然不会Tle了

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

using namespace std;

const double g=10.0,eps=1e-9;
const int N=200000+5,maxn=60+5,inf=0x3f3f3f3f;

ll num[N];
int Next[N],slen;
string str;

void getnext()
{
    int k=-1;
    Next[0]=-1;
    for(int i=1;i<slen;i++)
    {
        while(k>-1&&str[k+1]!=str[i])k=Next[k];
        if(str[k+1]==str[i])k++;
        Next[i]=k;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    int t,n;
    cin>>t;
    while(t--){
        cin>>n>>str;
        slen=str.size();
        getnext();
        ll ans=0;
        memset(num,0,sizeof num);
        for(int i=slen-1;i>=0;i--)
        {
            ll j=i;
            while(j!=-1)num[j]++,j=Next[j];
        }
        for(int i=0;i<slen;i++)ans=ans%mod+num[i]%mod;
        cout<<ans<<endl;
    }
    return 0;
}

时间: 2024-10-29 19:09:03

hdu3336 kmp的相关文章

[hdu3336]kmp(后缀数组)

题意:求字符串s的所有前缀出现次数之和. http://www.cnblogs.com/jklongint/p/4446117.html 思路:用kmp做,简单且效率高.以前缀结尾的位置分类,令dp[i]为以结尾位置在i的前缀数量,那么dp[i] = cnt(j)(j~i是前缀),而由kmp的next函数的转移性质,可得如下递推方程:dp[i] = dp[next[i]] + 1,把这个递推式不断展开,也就是i = next[i]不断迭代,那么+1的个数就是dp[i] = cnt(j)(j~i是

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 nu

HDU3336——KMP算法

题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头元素为起始,长度为next[i] 的前缀是相同的,那么方法就很容易了,对于每个j = i,沿着next[j]往前走,到0为止,记录走过的次数,就是前缀的出现次数. #include<cstdio> #include<algorithm> #include<cstring>

hdu3336解读KMP算法的next数组

查看原题 题意大致是:给你一个字符串算这里面所有前缀出现的次数和.比如字符串abab,a出现2次,ab出现2次,aba出现1次,abab出现1次.总计6次. 并且结果太大,要求对1007进行模运算. AC代码 #include <iostream> using namespace std; #include <string> string s; int n,Next[200005]; void getNext() { int len = n; Next[0]=-1; int i=0

hdu3336(Count the string)KMP的应用

题意:给一个字符串,计算所有前缀在字符串中出现的次数和. 解法:KMP计算出Next数组后,每个位置的Next数组不断往前递归,每次相应前缀次数就加1. 代码: /****************************************************** * author:xiefubao *******************************************************/ #pragma comment(linker, "/STACK:102400

[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

kmp算法专题总结

next数组的含义:next[i]表示以字符串s的第i个字符为结尾的后缀与s前缀匹配的长度 next数组也可以当做fail数组,即当模式串s[j]与串t[i]不匹配时,只要将j转换到next[j]继续匹配即可 在求s的next数组时,也用同样的原理,当s[j]与s[i]不匹配时,只要将j转换到next[j]继续匹配即可,当 注意此时模式串的首字母是s[0] poj3461 //next[i]表示和模式串第i位匹配失败时,再去和模式串第next[i]位匹配 #include<iostream>

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp