zoukankan      html  css  js  c++  java
  • HDU-3336-Count the string(扩展KMP)

    链接:

    https://vjudge.net/problem/HDU-3336

    题意:

    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.

    思路:

    计算s前缀出现的次数, 考虑扩展KMP的Nex数组, 从i位置开始从s0开始匹配的长度.
    即这些长度的前缀都出现过.累加和即可.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 2e5+10;
    const int MOD = 1e4+7;
    
    char s1[MAXN], s2[MAXN];
    int Next[MAXN], Exten[MAXN];
    
    void GetNext(char *s)
    {
        int len = strlen(s);
        int a = 0, p = 0;
        Next[0] = len;
        for (int i = 1;i < len;i++)
        {
            if (i >= p || i+Next[i-a] >= p)
            {
                if (i >= p)
                    p = i;
                while (p < len && s[p] == s[p-i])
                    p++;
                Next[i] = p-i;
                a = i;
            }
            else
                Next[i] = Next[i-a];
        }
    }
    
    void ExKmp(char *s, char *t)
    {
        int len = strlen(s);
        int a = 0, p = 0;
        GetNext(t);
        for (int i = 0;i < len;i++)
        {
            if (i >= p || i + Next[i-a] >= p)
            {
                if (i >= p)
                    p = i;
                while (p < len && s[p] == t[p-i])
                    p++;
                Exten[i] = p-i;
                a = i;
            }
            else
                Exten[i] = Next[i-a];
        }
    
    }
    
    int main()
    {
        int t, n;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d", &n);
            scanf("%s", s1);
            GetNext(s1);
            int res = 0;
            for (int i = 0;i < strlen(s1);i++)
                res = (res+Next[i])%MOD;
            printf("%d
    ", res);
        }
    
        return 0;
    }
    
  • 相关阅读:
    浮点数精度问题(2.01.1=0.8999999999)
    创建android的模拟器时屏幕的大小设置
    hdu 3038 How Many Answers Are Wrong(并查集)
    hdu 3635 Dragon Balls(并查集)
    hdu 1598 find the most comfortable road(并查集+暴力搜索)
    hdu 1671 Phone List (字典树)
    hdu 3047Zjnu Stadium(并查集)
    hdu 1247 Hat’s Words(字典树)
    后缀数组——处理字符串的有力工具
    hdu 2473 JunkMail Filter(并查集+虚拟节点)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11593766.html
Copyright © 2011-2022 走看看