hdu 3336 Count the string

17 篇文章 0 订阅
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 

【题意】给你一个字符串x,它有strlen(x)个前缀,问你所有前缀出现的次数总和,多组输入。

代码简单,先上代码。
【代码】

#include <bits/stdc++.h>
using namespace std;
int main (void)
{
    int n,m,i,j,k,l;
    char s[222222];
    cin>>m;
    while(m--&&cin>>n)
    {
        cin>>s;
        k=0;
        for(i=0; i<n; i++)
        {
            k%=10007;
            l=i;
            j=0;
            while(l<n)
                if(s[l++]==s[j++])
                    k++;
                else break; 
        }
        cout<<k%10007<<endl;
    }
    return 0;
}

【分析】看不懂不急,我们慢慢看。首先是s,明显是存储字符串,k就是答案,然后关于输入部分就不解释了,关键就是那个循环的部分,那个if的条件意思是:如果字符串的s【0,j】和字符串s【i,l】相同,说明s【0,j】这个前缀出现了一次我们把他加上就行。然后i一循环答案就出来了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值