zoukankan      html  css  js  c++  java
  • hdu3336 Count the String KMP+DP

    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. 
    InputThe 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. 
    OutputFor 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【i】表示的是以i为结尾的字符串前后缀长度重叠的最长的长度

    在按照next移动的过程中,某一个前缀会重复出现

    所以我们只需要在字符串中按照next数组来跳跃,慢慢扩展【嗯语言好难表达】

    然后用dp  状态转移方程: cnt[i] = cnt[next[i]] + 1

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #define inf 0x3f3f3f3f
    
    using namespace std;
    
    int n, t;
    char s[200005];
    int nnext[200005], cnt[200005];
    
    void getnext()
    {
        nnext[0] = 0;
        int k = -1;
        for(int i = 1; i < n; i++){
            while(k >= 0 && s[k + 1] != s[i]){
                k = nnext[k];
            }
            if(s[k + 1] == s[i]){
                k++;
            }
            nnext[i] = k;
        }
        /*nnext[0] = -1;
        int j = 0,  k = -1;
    
        while(j < n - 1){
            if(k == -1 || s[j] == s[k]){
                ++j;
                ++k;
                nnext[j] = k;
            }
            else{
                k = nnext[k];
            }
        */
    }
    
    int main()
    {
        scanf("%d", &t);
        while(t--){
            scanf("%d",&n);
            scanf("%s", s + 1);
            memset(nnext, 0,sizeof(nnext));
            nnext[1] = 0;
            int k = 0;
            for(int i = 2; i <= n; i++){
                while(k > 0 && s[k + 1] != s[i]){
                    k = nnext[k];
                }
                if(s[k + 1] == s[i]){
                    k++;
                }
                nnext[i] = k;
            }
            //getnext();
            memset(cnt, 0, sizeof(cnt));
            int sum = 0;
            for(int i = 1; i <= n; i++){
                cnt[i] = (cnt[nnext[i]] + 1) % 10007;//以 i 结尾的串中所有前缀的计数和
                sum = (sum + cnt[i]) % 10007;
            }
            printf("%d
    ",sum);
        }
    	return 0;
    }
    


    错点:

    刚开始一直出错,因为之前用的是优化了的next数组 感觉不能用优化过的

    还有就是发现最后sum那里取模那里写的是+= 这样应该也是不行的 写的时候都要注意一点

    memset居然也手误写成了sizeof居然都没发现也没有报错

    改成了没有优化的版本以后不知道为什么样例也过不了

    就去借鉴了一下别人的代码

    之后开始一直TLE

    把字符串改成从1开始就过了

    应该是后面可能因为我起始的next设置为-1 死循环了


    总结:

    嗯对于算法的理解还要深刻一点 不能只局限在写得出基础的

    经常变一下型就不会实现了

    emm这个提高方向有点困难感觉不知道怎么弄


  • 相关阅读:
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    CF #505 B Weakened Common Divisor(数论)题解
    HDU 6425 Rikka with Badminton(组合问题签到)题解
    ZOJ 2747 Paint the Wall(离散化+暴力)题解
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643436.html
Copyright © 2011-2022 走看看