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;
    }
  • 相关阅读:
    【Android API】Android 4.1 API官方文档详解
    【Android开发】Android Host详解(翻译自官方文档)
    Android调试桥-Android Debug Birdge详解
    冰淇淋三明治 (Android 4.0)介绍
    Android 4.1的新特性介绍
    【Android UI】如何做一个纯粹的Android app UI 设计
    【Android UI】Android颜色系大全
    【Android开发】交互界面布局详解
    阿里卖电影票 架构思路
    操作系统 虚拟内存 、分段、分页的理解
  • 原文地址:https://www.cnblogs.com/shenben/p/5748208.html
Copyright © 2011-2022 走看看