zoukankan      html  css  js  c++  java
  • HDU 3336 Count the string(next数组运用)

    Count the string
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 14096    Accepted Submission(s): 6462


    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


    题意:统计一个字符串的所有前缀在字符串中出现的次数的和

    分析:next数组的运用,因为next数组可以表示以当前坐标的前一个字符结尾的字符串的前缀和后缀有多少个字符相等

    举个例子,有一个字符串

    abcdabdabcdab

    i            0    1    2    3    4    5    6    7    8    9    10   11   12   13

                 a    b    c    d    a    b   d    a    b    c    d     a     b

    nex[i]  -1    0    0    0    0    1    2    0    1    2    3     4     5     6

    dp[i]     0    1    1    1    1    2    2    1    2    2    2     2     3     3

    dp[i]=dp[nex[i]]+1

    dp[i]表示第i个字母出现次数(与前缀匹配情况下),+1代表的是前缀出现次数加1,dp总和即为前缀出现次数和

    #include<iostream>
    #include<string.h>
    #define inf 10007
    using namespace std;
    int nex[200001], dp[200001];
    void getnext(char s[],int len)
    {
        int i=0,j=-1;
        nex[0]=-1;
        while(i<len)
        {
            if(j==-1||s[i]==s[j])
                nex[++i]=++j;
             else j=nex[j];
        }
    }
    int main()
    {
        int t,n;
        scanf("%d",&t);
        char s[200001];
        while(t--)
        {
            scanf("%d%s",&n,s);
            getnext(s,n);
            int cnt=0;
            dp[0]=0;
            for(int i=1;i<=n;i++)
            {
                dp[i]=(dp[nex[i]]+1)%inf;
                cnt+=dp[i]%inf;
            }
           printf("%d
    ",cnt%inf);
        }
        return 0;
    }
    
  • 相关阅读:
    5.4 类型转换
    IntelliJ IDEA 创建Web项目(全教程)
    Spring IoC 依赖注入的方法大全 XML配置方式
    20秒教你如何写maven2的pom文件的依赖包
    数据库面试题之COUNT(*),COUNT(字段),CONUT(DISTINCT 字段)的区别
    Oracle中的单行函数
    Select 子句后的别名,在where条件中不能使用
    Oracle SQL*Plus命令
    使用js请求Servlet时的路径
    idea 创建运行web项目时,报错: Can not issue executeUpdate() for SELECTs解决方案
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9502675.html
Copyright © 2011-2022 走看看