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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 零基础学Java(2)数据类型与变量

    零基础学Java(2)数据类型与变量前言Java是一种强类型语言。这就意味着必须为每一个变量声明一种类型。在Java中,一共8种基本类型,其中有4种整型、2种浮点型、1种字符串类型char(用于表示Unicode编码的代码单元)和1种

    2022年8月7日
    4
  • 使用decode函数

    使用decode函数Decode函数使用:Oracle的decode函数蛮有意思,是oracle独有的,国际标准SQL中并没有decode函数。语法DECODE(col|expression,search1,result1[,search2,result2,…,][,default])例子SELECTproduct_id,DECODE(warehouse_id,1…

    2022年7月25日
    7
  • PL/SQL连接oracle数据库

    PL/SQL连接oracle数据库

    2021年12月3日
    54
  • 大数据简介

    大数据简介目录1、大数据概述传统数据处理介绍2、什么是大数据?(BigData)3、传统数据与大数据的对比4、大数据的特点数据集主要特点 其他特征 传统数据与大数据处理服务器系统安装对比5、大数据生态系统新技术6、大数据技术为什么快?大数据技术快的原因1、大数据概述 传统数据处理介绍 数据来源:…

    2022年5月4日
    47
  • 使用高通QXDM工具抓取Modem Log的操作方法(独家!)

    使用高通QXDM工具抓取Modem Log的操作方法(独家!)高通QXDM工具包下载链接如下:链接:https://pan.baidu.com/s/1rRNicFvlRSstUhka2JSiOg密码:pssp具体操作步骤如下:1、安装工具包里的QPST和QXDM软件。2、打开工具包里的3.dmc文件来启动QXDM软件,启动过程中会自动打开QPSTConfig。3、打开设备的开发者选项和USB调试选项,将设备连接到PC,取消”媒…

    2022年10月2日
    2
  • pycharm 模板_pycharm基础代码

    pycharm 模板_pycharm基础代码在Pycharm中编码时,当我们输入main再按下Tab键,编辑器会自动出现如下代码块:if__name__==’__main__’:类似地,如果我们有一大段代码要经常重复使用,可以将这段代码设置成一个模版,通过自定义的指令+Tab键直接导入代码。比如我们有如下一段代码:fromPyQt5.Qtimport*classWindow(QWidget):def__init__(self):super().__init__()

    2022年8月25日
    7

发表回复

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

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