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

    Count the string
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

    ab

    aba

    abab

    abab中共有六个子串是前缀a a ab ab aba abab

    所以答案为6

    利用kmp中的匹配原理可以完美的解决此题

    a---------d-----

       -----a---------d

                   i         j

    如上所示,假设两串字符完全相等,next[j]=i,代表s[1...i]==sum[j-i+1....j],这一段其实就是前缀

    i~j之间已经不可能有以j结尾的子串是前缀了,不然next【j】就不是 i 了

    设dp【i】:以string[i]结尾的子串总共含前缀的数量

    所以dp[j]=dp[i]+1,即以i结尾的子串中含前缀的数量加上前j个字符这一前缀

    /*
    * kmp+dp
    */
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    const int N = 200005;
    
    char pat[N];
    int nnext[N], dp[N];
    
    void indexNext() {  ///从第二个开始构造!!!挺巧妙的,学习了!!!
        int k = 0;
        nnext[1] = 0;
        for (int i=2; pat[i]; ++i) {
            while (k && pat[k+1]!=pat[i])
                k = nnext[k];
            if (pat[k+1] == pat[i])
               ++k;
            nnext[i] = k;
        }
    }
    
    int solve(int len) {
        int sum = 0;
        //getnext(strlen(pat));
        indexNext();
        for (int i=1; i<=len; ++i) dp[i] = 1;
        dp[0] = 0;
        for (int i=1; i<=len; ++i) {
            dp[i] += dp[nnext[i]];
            sum += dp[i];
            sum %= 10007;
        }
        return sum;
    }
    
    int main() {
        int t;
        scanf ("%d", &t);
        pat[0] = '#';
        while (t--) {
            int n;
            scanf ("%d%s", &n, pat+1);
            printf ("%d
    ", solve(n));
        }
        return 0;
    }
    




  • 相关阅读:
    【还是畅通工程 HDU
    【畅通工程 HDU
    【hdu 2544最短路】【Dijkstra算法模板题】
    【Isabella Message】 【SPOJ
    【统计难题】【HDU
    【I'm Telling the Truth】【HDU
    Arranging Your Team HDU
    JavaScript和jQuery改变标签内容
    JavaScript获取地址栏内容
    JavaScript遍历IP段内所有IP
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717928.html
Copyright © 2011-2022 走看看