zoukankan      html  css  js  c++  java
  • hdu3336

    Count the string

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8594    Accepted Submission(s): 3969


    Problem 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
     
    Author
    foreverlin@HNU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1711 1686 3746 1358 3341 
    TLE代码:
    #include<cstdio>
    #include<iostream>
    using namespace std;
    #define N 200010
    int T,lens,lenp,nextt[N];
    char s[N],p[N];
    inline void get_next(){
        int i=0,j=-1;
        nextt[i]=j;
        while(i<lenp){
            if(j==-1||p[i]==p[j]){
                i++;j++;
                nextt[i]=j;
            }
            else j=nextt[j];
        }
    }
    inline int kmp(){
        get_next();
        int i(0),j(0),ans(0);
        while(i<lens&&j<lenp){
            if(j==-1||s[i]==p[j]){
                i++;j++;
            }
            else j=nextt[j];
            if(j==lenp) ans++,j=nextt[j];
        }
        return ans%10007;
    }
    int main(){
        scanf("%d",&T);
        while(T--){
            scanf("%d",&lens);
            scanf("%s",s);
            int sum=0;
            for(int i=0;i<lens;i++){
                fill(p,p+i+1,0);
                for(int j=0;j<=i;j++) p[j]=s[j];
                lenp=i+1;
                sum+=kmp();
                sum%=10007;
            }
            printf("%d
    ",sum%10007);
        }
        return 0;
    }
    题解:
    依次求字符串匹配数,求和,取模
    水水的KMP
     
    AC代码:
    #include<cstdio>
    #include<iostream>
    using namespace std;
    #define N 200010//数组开的大大的 
    #define mod 10007//注意取模 
    int T,len,Next[N];
    char s[N],p[N];
    inline void get_next(){//next是关键字,所以用Next[] 
        int i=0,j=-1;
        Next[i]=j;
        while(i<len){
            if(j==-1||s[i]==s[j]){
                i++;j++;
                Next[i]=j;
            }
            else j=Next[j];
        }
    }
    int main(){
        scanf("%d",&T);
        while(T--){
            scanf("%d",&len);
            scanf("%s",s);
            get_next();//每组数据做一次求好了,否则TLE
            int sum=0;
            for(int i=1,j;i<=len;i++){
                j=i;
                while(j) sum=(sum+1)%mod,j=Next[j];
            } 
            printf("%d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    浅谈Web前端开发未来的8个的趋势
    人工智能必备数学知识学习笔记7:矩阵的应用和更多矩阵相关的高级话题
    人工智能必备数学知识学习笔记6:矩阵(矩阵不只是mn个数字)
    人工智能必备数学知识学习笔记5:向量的高级话题
    人工智能必备数学知识学习笔记4:零向量
    人工智能必备数学知识学习笔记3:向量的基本运算
    人工智能必备数学知识学习笔记2:向量
    人工智能必备数学知识学习笔记1:线性代数与机器学习
    Python入门学习笔记12:pythonnic补充
    Python入门学习笔记11:原生爬虫
  • 原文地址:https://www.cnblogs.com/shenben/p/5748208.html
Copyright © 2011-2022 走看看