zoukankan      html  css  js  c++  java
  • HDU 3336 Count the string

    题意:字符串的每个前缀在原字符串中出现的次数和

    思路:我们求出字符串的next数组,对于每个前缀,最少有一个,然后对于每个位置我们看他的next数组值是否大于0,如果大于0,ans++

    代码:(有点看不懂网上得到通解)

    #include <cstdio>
    #include <cstring>
    const int maxn=2e5+7;
    const int MOD=10007;
    char a[maxn];
    int len;
    int next[maxn];
    
    void getNext(char T[])
    {
        int j, k;
        j = 0;
        k = -1;
        next[0] = -1;
        while(j < len)
            if(k == -1 || T[j] == T[k])
                next[++j] = ++k;
            else
                k = next[k];
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&len);
            scanf("%s",a);
            memset(next,0,sizeof(next));
            getNext(a);
    //        for(int i=0;i<=len;i++){
    //            printf("%d ",next[i]);
    //        }
    //        puts("");
            int ans=len%MOD;
            for(int i=1;i<=len;i++){
                if(next[i]>0)ans++;
                ans%=MOD;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    前端工程化
    前端模块化CommonJS&ES6
    为什么浮点型运算结果会有误差?
    RequestAnimationFrame知多少?
    CSS三栏布局
    秋招面试
    实现Storage
    Angular
    TypeScript
    微服务架构设计模式
  • 原文地址:https://www.cnblogs.com/lalalatianlalu/p/9125640.html
Copyright © 2011-2022 走看看