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)
上一篇 2022年7月23日 下午7:16
下一篇 2022年7月23日 下午7:16


相关推荐

  • 编辑器、编译器、文件、IDE等常见概念辨析

    编辑器、编译器、文件、IDE等常见概念辨析

    2021年10月6日
    54
  • Java常用的设计模式

    Java常用的设计模式

    2021年5月5日
    111
  • 矩阵范数不等式_范数三角不等式取等号

    矩阵范数不等式_范数三角不等式取等号矩阵范数不等式∣∣A∣∣2≤∣∣A∣∣1∣∣A∣∣∞||A||_2\le||A||_1||A||_{\infty}∣∣A∣∣2​≤∣∣A∣∣1​∣∣A∣∣∞​证明引理1严格对角占优的矩阵行列式为正n维实矩阵A,满足aii>∑1≤j≤n,j≠i∣aij∣a_{ii}\gt\sum_{1\lej\len,j\nei}|a_{ij}|aii​>1≤j≤n,j​=i∑​∣aij​∣则称A为严格对角占优的矩阵,而∣A∣>0|A|>0∣A∣>0引理1的证

    2026年1月25日
    7
  • SeaJS入门

    SeaJS入门SeaJS 过时了 所谓的过时 并不是指现在就不能用了 而是说出现了明显更加先进的理念 或者标准 这会导致未来它的使用场景大为减少 整体趋势已经步入衰落 知乎回答 https www zhihu com question 还是记录下 seajs config base

    2026年3月18日
    2
  • Python 二次开发 AutoCAD 简介「建议收藏」

    Python 二次开发 AutoCAD 简介「建议收藏」一、前沿cad是python是ActiveX是pyautocad模块由俄罗斯工程师开发,因参考实例较少,工程需要,现将模块中一些基本的用法,做出简要说明,叙述力求简洁明了,因个人水平有限,文中难免有所疏漏,还请各位大神不吝批评指正。…

    2022年6月5日
    41
  • 计算机网络vlan的作用,计算机网络之九:VLAN

    计算机网络vlan的作用,计算机网络之九:VLAN一:什么是VLAN广播在网络中起着非常重要的作用,如发现新设备,调整网络路径,IP地址租赁等,许多网络协议都要用到广播。然而,随着网络内计算机数量的增多,广播包的数量也会急剧增加,当广播包的数量占到通讯总量的30%时,网络的传输效率将会明显下降。所以当局域网内的计算机达到一定数量后,通常采用划分VLAN(虚拟局域网)的方式将网络分隔开来。将一个大的广播域划分为若干个小的广播域,以减小广播可能造成的…

    2022年8月10日
    7

发表回复

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

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