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


相关推荐

  • lcd1602使用手册_lcd液晶屏工作原理

    lcd1602使用手册_lcd液晶屏工作原理1602液晶也叫1602字符型液晶,它是一种专门用来显示字母、数字、符号等的点阵型液晶模块。1602LCD是指显示的内容为16X2,即可以显示两行,每行16个字符液晶模块(显示字符和数字)。lcd1602引脚状态字的说明:RAM映射地址:控制接口的时序:1.读的时序2.写的时序3.时序的相关参数读状态:RS=L,R/W=H,EN=H读数据:RS=H,…

    2022年9月23日
    3
  • Quartus II 13.1的安装及使用

    Quartus II 13.1的安装及使用QuartusII的安装及使用前言一、QuartusII的下载二、QuartusII的安装三、QuartusII的注册四、QuartusII的使用(一)相关驱动的配置(二)使用流程的认识(三)使用过程总结前言本文章是对QuartusII13.1的安装及使用方法的介绍说明。一、QuartusII的下载百度网盘下载链接:https://pan.baidu.com/s/1a9d-bq9RZmWrRV542X4IEA提取码:ifte说明:本链接来自于正点原子官方资料下载二、

    2022年10月16日
    2
  • mysql的UUID获取上一篇下一篇(上一条 下一条)应用实例[通俗易懂]

    mysql的UUID获取上一篇下一篇(上一条 下一条)应用实例[通俗易懂]先讲原理:有上一篇下一篇(上一条下一条),肯定是在:搜索条件下,排序规则固定的场景下,得到的一个查询集合(列表)中的一个效果。1.我们在这两个条件(搜索条件where排序规则order),给查询结果集给利用rownum(一个顺序自增的标号)2.查询出目标uuid的rownum值x.3.查询上一条和下一条:rownum=x-1的uuid得到上一条rownum=x+1的uuid得到下一条实际应用:一个固定的检索条件固定排序的查询:SELECT bn.*FROM b

    2022年8月9日
    5
  • CC2530: ZigBee协议栈实践例程(一)

    CC2530: ZigBee协议栈实践例程(一)1.ZigBee版本      ZigBee是ZigBee联盟建立的技术标准。第一个ZigBee协议栈规范于2004年发布,称为ZigBee2004或者ZigBee1.0;第二个ZigBee协议栈规范于2006年发布,称为ZigBee2006;第三个ZigBee协议栈规范于2007年发布,称为ZigBee2007;然后呢?现在是2018年了。。。2.Z-Stack版本    …

    2022年5月28日
    29
  • python py库安装_pygame怎么用

    python py库安装_pygame怎么用最详细python安装库的方法!!!到此第一种方法结束第二种方法(在终端安装库)win+r:打开运行输入cmd然后cd到你的python解释器下的scripts中(比如我的路径是D:\ProgramFiles(x86)\python\Scripts)找到这个路径后下面就开始安装cd到了scripts中就输入以下三个任意一个进行库的安装安装pipinstall………

    2022年10月2日
    3
  • idea激活码2021【2021.10最新】

    (idea激活码2021)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1STL5S9V8F-eyJsaWNlbnNlSWQi…

    2022年3月27日
    142

发表回复

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

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