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;
    }
    




  • 相关阅读:
    面试官没想到一个Volatile,我都能跟他扯半小时
    面试官:你经历过数据库迁移么?有哪些注意点和难点?
    【逼你学习】让自制力提升300%的时间管理方法、学习方法分享
    面试官:说一下内存溢出排查过程和工具?我...
    大厂需求研发流程,进去前了解一波?
    面试必问:分布式锁实现之zk(Zookeeper)
    Redis之分布式锁实现
    大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
    【面经分享】互联网寒冬,7面阿里,终获Offer,定级P6+
    我找到了Dubbo源码的BUG,同事纷纷说我有点东西
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717928.html
Copyright © 2011-2022 走看看