3336 Count the string

Count the string

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


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


题目大意:给一个字符串s,问s的前缀在s中出现的次数的总和

比如例子:abab

前缀         在s中出现的次数

a                    2

ab                  2

aba                1

abab              1

所以结果为6

我看到这个题目的第一反应是用循环求字符串s的每个子串next数组然后再求kmp,提交超时。(kmp算法的时间复杂度为O(n+m),n为字符串s的长度,m为要求next数组的字符串的长度)

然后想了一下其实并不用求每个子串的next数组,只要求s的next数组就好了,提交还是超时。

然后想了一下s的每个子串的个数为s的长度,然后就是和每个子串相等的个数,我想的是next数组有值的答案就要加一,提交AC。后来看杭电的 Discuss 发现杭电的数据较弱,比如abababab的答案应该为20,但是我的代码得出的结果是16,结果还是AC了。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn = 200000+5;
int ans;
int Next[maxn];
void getNext(string s)
{
    int ls=s.size();
    Next[0]=-1;
    Next[1]=0;
    for(int i=2;i<=ls;i++){
        if(s[i-1]==s[Next[i-1]]) Next[i]=Next[i-1]+1;
        else{
            int t=Next[i-1];
            while(s[t]!=s[i-1]){
                t=Next[t];
                if(t==-1) break;
            }
            Next[i]=t+1;
        }
    }
}
void kmp(string a,string b)
{
    int la=a.size();
    int lb=b.size();
    int i=0,j=0;
    while(i<la && j<lb){
        if(a[i]==b[j] || j==-1) i++,j++;
        else{
            while(a[i]!=b[j]){
                j=Next[j];
                if(j==-1) break;
            }
        }
        if(j==lb) ans++,j=0;
    }
}
int main()
{
    cin.sync_with_stdio(false);
    int t,n;
    string a;
    cin>>t;
    while(t--){
        cin>>n>>a;
        ans=n;
        getNext(a);
        for(int i=0;i<=n;i++){
            if(Next[i]>0) ans++;
        }
        cout<<ans%10007<<endl;
    }
    return 0;
}</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值