• 【HDU 3336 Count the string】


    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 11607    Accepted Submission(s): 5413
    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

    Author

    foreverlin@HNU

    Source

    HDOJ Monthly Contest – 2010.03.06

    Recommend

    lcy

    题解:

          ①首先发现如果长度为i前缀出现了,那么长度小于i的前缀也就随之出现了至少一次。

          ②考虑KMP中的next数组表示了最长前后缀的性质,可以使用如下DP:
                 num[i]表示序列a[0~i]中出现了好多个前缀(不论长度)

                 转移方程式:num[i]=num[next[i]]+1 (+1是自己匹配自己)

         ③这样做的原因:next指向最长前缀,那么相同的当前后缀可以再构造一模一样的解。

         ④至此发现其实num[i]的定义是有缺陷的,只是为了方便理解。因为为了不重复计算答案,必须不能保存以前的答案(即答案不是num[m])。

    #define M 10007
    #include<stdio.h>
    #define go(i,a,b) for(int i=a;i<=b;i++)
    const int N=200003;int T,m,j,f[N],num[N],ans;char P[N];
    int main()
    {
    	scanf("%d",&T);
    	while(ans=0,T--&&scanf("%d%s",&m,P))
    	{
    		go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
    		go(i,1,m)(ans+=((num[i]=num[f[i]]+1)%=M))%=M;
    		printf("%d
    ",(ans%M+M)%M);
    	}
    	return 0;
    }//Paul_Guderian
    

    Turns out,real life's a little bit more complicated than a slogan on a bumper sticker……

                                                                             ————Judy·Hopps

  • 相关阅读:
    [oracle]ORA-38707: Media recovery is not enabled.
    [ORACLE]oracle 如何解决高水平线问题
    [ORACLE]Oracle ASH报告生成和性能分析
    [ORACLE]oracle 表连接的几种方式
    [ORACLE] ORACLE 查看执行计划 的方式
    [ORACLE] ORACLE访问数据的方法
    [ORACLE]ORACLE19 client 安装
    [ORACLE]查看SQL绑定变量具体值 查看SQL绑定变量值
    [ORACLE]锁 enq: TX
    将通过find命令找到的文件拷贝到一个新的目录中
  • 原文地址:https://www.cnblogs.com/Damitu/p/7682991.html
Copyright © 2020-2023  润新知