zoukankan      html  css  js  c++  java
  • kmp dp hdu 3336

    Count the string

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


    http://acm.hdu.edu.cn/showproblem.php?pid=3336

    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
    #include<stdio.h>//网上大佬的代码; 
    #include<iostream>
    #include<string.h>
    #include<stack>
    using namespace std ;
    
    const int maxn = 1e6+5;
    int Next[maxn];
    char str[maxn];
    char mo[maxn];
    int dp[maxn];
    int n1,n2;
    
    void GetNext() 
    {
        int i=0,j=-1;
        while(i<n2)
        {
            if(j==-1||mo[i]==mo[j]) {++i,++j,Next[i]=j;}
            else j=Next[j];
        }
        return ;
    }
    
    /*int kmp()
    {
        int cnt=0;
        int i=0,j=0;
        while(i<n1)
        {
            if(j==-1||str[i]==mo[j]) i++,j++;
            else j=Next[j];
            if(j==n2)
            {
                cnt++;
                j=0;
            }
        }
        return cnt;
    }*/
    
    int ans[maxn];
     
    int main()
    {
        int cnt;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%s",&n2,mo);//n2为字符串长度,mo为字符串; 
            Next[0]=-1;
            
            GetNext();
            
            int ans=0;
            
            dp[0]=0;
            
            for(int i=1;i<=n2;i++){
                printf(" %d ###
    ",Next[i]); 
            }
            
            for(int i=1;i<=n2;i++)
            {
                dp[i]=dp[Next[i]]+1;//用动态规划的思维;每一个的值为该后缀的值加一,加一表示其本身,next[i]表示有 
                                    //后缀与前缀相同,则后缀与前缀的值相同;前缀的值已经算了; 
                ans=(ans+dp[i])%10007; 
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    #include<cstdio>
    #include<cstring>
    const int maxn=2e5+3;
    char str[maxn];
    int Next[maxn],dp[maxn];
    int t,len;
    void getNext(){
        Next[0]=-1;
        int j=0;
        int k=-1;
        while(j<len){
            if(k==-1||str[k]==str[j]){
                k++;
                j++;
                Next[j]=k;
            }else{
                k=Next[k];
            }
        }
        return;
    }
    int main(){
        scanf("%d",&t);
        while(t--){
            scanf("%d%s",&len,str);
            getNext();
            int ans=0;
            dp[0]=0;
            for(int i=1;i<=len;i++){
                dp[i]=dp[Next[i]]+1;
            //    printf("%d
    ",dp[i]); 
                ans=(ans+dp[i])%10007;
            }
            printf("%d
    ",ans);
        }
        return 0;
    } 
  • 相关阅读:
    Git fetch和git pull的区别
    git add 命令详解
    第1章——算法在计算机中的作用
    Mysql数据库中的计数器表实时更新
    Windows 7 搭建 nodejs、npm、express 环境
    设计模式之工厂模式
    设计模式之单例模式(Singleton Pattern)
    java实现合并两个已经排序的列表
    Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端
    sqlserver乱码问题解决
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/11590521.html
Copyright © 2011-2022 走看看