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


相关推荐

  • @transactional的使用_@transactional注解默认的回滚方式

    @transactional的使用_@transactional注解默认的回滚方式@Transactional是声明式事务管理编程中使用的注解1.添加位置1)接口实现类或接口实现方法上,而不是接口类中。2)访问权限:public的方法才起作用。@Transactional注解应该只被应用到public方法上,这是由SpringAOP的本质决定的。系统设计:将标签放置在需要进行事务管理的方法上,而不是放在所有接口实现类上:只读的接口就不需要事务管…

    2022年9月30日
    2
  • 智能优化算法简介

    智能优化算法简介智能优化算法:受人类智能、生物群体社会性或自然现象规律的启发。主要包括:(1)遗传算法:模仿自然界生物进化机制(2)差分进化算法:通过群体个体间的合作与竞争来优化搜索(3)免疫算法:模拟生物免疫系统学习和认知功能(4)蚁群算法:模拟蚂蚁集体寻径行为(5)粒子群算法:模拟鸟群和鱼群群体行为(6)模拟退火算法:源于固体物质退火过程(7)禁忌搜索算法:模拟人类智力记忆过程(8)…

    2022年5月10日
    59
  • 数据结构之二叉树的前序遍历、中序遍历、后序遍历、层序遍历「建议收藏」

    数据结构之二叉树的前序遍历、中序遍历、后序遍历、层序遍历「建议收藏」最近也是在准备笔试,由于没有系统的学过数据结构,所以每次在考到二叉树的遍历的时候都是直接跪,次数多了也就怒了,前些天也是准备论文没时间整这些,现在提交了,算是稍微轻松点了,所以花了半天的时间来学了下二叉树。现在记下来,以便后序查阅。一、二叉树的遍历概念  1. 二叉树的遍历是指从根结点触发,按照某种次序依次访问二叉树中所有结点,使得每个结点被访问一次且仅被访问一次。(1).前(

    2025年11月17日
    5
  • c++线程间通信_c语言两个线程如何通信

    c++线程间通信_c语言两个线程如何通信c++线程间通过PostThreadMessage和GetMessage函数进行通信,下面用代码演示两个线程间的通信://ConsoleApplication1.cpp:定义控制台应用程序的入口点。//#include<stdio.h>#include<windows.h>usingnamespacestd;DWORDWINAPIThreadFun1(LPVOIDparam);DWORDWINAPIThreadFun2(LPVOIDpara

    2022年10月6日
    2
  • WebStorm如何设置字体大小

    WebStorm如何设置字体大小由于最近要练习一些js代码,特地装了WebStorm,但是发现字体太小,因此将已知的两种方法整理出来。一、Ctrl+滚动滑轮调整字体大小File—>Settings(Ctrl+Alt+s)—>Editor—>General—>Change font size(Zoom)……前面的方框打对勾。如下图点击ok即可。在编辑代码页面Ctrl+滚动滑轮…

    2022年6月13日
    42
  • 华为电脑如何投屏到电视linux,华为手机如何投屏到电脑上?手把手教你,无线投屏怎么做…「建议收藏」

    原标题:华为手机如何投屏到电脑上?手把手教你,无线投屏怎么做经常宅在家里想要看电影,手机屏幕太小影响观看体验,或者需要投屏到电脑上,方便办公。这个时候应该怎么办?如果你使用的是华为手机,可以直接投屏到电视、电脑上,这里就来手把手教你如何操作。1、打开功能华为手机开启无线投屏的方式有2种:第1种是在手机设置内,选择更多链接,进入之后就可以开启无线投屏的功能;第2种是直接下拉通知栏,然后开启手机投…

    2022年4月9日
    119

发表回复

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

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