hdu 3336 Count the string(kmp应用)

hdu 3336 Count the string(kmp应用)ProblemDescriptionItiswellknownthatAekdyCoinisgoodatstringproblemsaswellasnumbertheoryproblems.Whengivenastrings,wecanwritedownallthenon-emptyprefixesofthisstring.

大家好,又见面了,我是你们的朋友全栈君。

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
 

给你一个字符串算这里面所有前缀出现的次数和。比如字符串abab,a出现2次,ab出现2次,aba出现1次,abab出现1次。


  1. #include <iostream>

    #include <string.h>

    #include <stdio.h>

    using namespace std;

    int Next[200010],dp[200010]; //dp[i]=以第i个字符结尾的串中所有前缀的计数和

    char str1[200010];

    void GetNext(int len,char *s)

    {

        Next[1]=0;

        int i=1;

        int j=0;

        dp[0]=0;

        while (i<=len)

        {

            if(j==0 || s[i]==s[j])

            {

                dp[i]=(dp[j]+1)%10007;

                i++;

                j++;

                if(s[i]==s[j])

                    Next[i]=Next[j];

                else

                    Next[i]=j;

            }

            else

                j=Next[j];

        }

    }

    int main()

    {

        int t,n,ans;

        scanf(“%d”,&t);

        while (t–)

        {

            ans=0;

            scanf(“%d”,&n);

            getchar();

            scanf(“%s”,str1+1);

            GetNext(n,str1);

            for (int i=1; i<=n; i++)

            {

                ans=(ans+dp[i])%10007;

            }

            

            printf(“%d\n”,ans);

        }

        return 0;

    }


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163382.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • LC5.最长回文字串[通俗易懂]

    LC5.最长回文字串[通俗易懂]中心扩展法主要思路是每次选一个中点,然后向两边扩展,找出以该中点对应的最长的回文串的长度,然后维护一个全局的最长回文串长度变量。对于奇偶数长度的不同处理方式:expandAroundCenter方法中如果传入同一个点的索引,则表示以该点为中心,对应着探索字符串长度为奇数的情况如果传入两个点的索引,则表示以这两个点之间为中心,对应着探索字符串长度为偶数的情况/***@Classn…

    2022年7月24日
    5
  • 阿里云动态域名_阿里动态域名解析

    阿里云动态域名_阿里动态域名解析前言该脚本的代码大部分是参考自阿里云的官方帮助文档。1,脚本语言使用的是python,我个人只是了解python,没有太深入的知识功底2,脚本代码我会尽量详细地添加注释说明,有问题欢迎留言

    2022年8月5日
    4
  • mysql 类型长度_数据库decimal类型长度

    mysql 类型长度_数据库decimal类型长度1个字节=8位tinyint为一个字节2的8次方=256所以最多存储到256日期和时间数据类型MySQL数据类型含义date3字节,日期,格式:2014-09-18time3字节,时间,格式:08:42:30datetime8字节,日期时间,格式:2014-09-1808:42:30timestamp4字节,自动存储记录修改的时间year1字节,年份数值数据类型整型MySQL数据…

    2022年9月17日
    0
  • 池的概念和EPOLLONESHOT事件(读Linux高性能服务器)

    池的概念和EPOLLONESHOT事件(读Linux高性能服务器)

    2021年9月17日
    69
  • Windows Server 2012 DHCP 高可用性

    Windows Server 2012 DHCP 高可用性

    2022年3月12日
    97
  • HikariPool配置详解

    HikariPool配置详解HikariPool较佳配置 hikari: connection-timeout:60000 validation-timeout:3000 idle-timeout:60000 login-timeout:5 max-lifetime:60000 maximum-pool-size:10 minimum-idle:10 read-only:falsehikari各参数解释https://github.com/.

    2022年6月23日
    442

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号