zoukankan      html  css  js  c++  java
  • NOI 2014 动物园

    经过上午的学习,终于对KMP有了初步的了解
    这里就是在get_sum的时候进行了一下小小的变形。
    (听说要写快速乘,网上copy了一份。 留着以后用吧)

    http://uoj.ac/problem/5
    UOJ数据挺好的,重点是可以看别人的程序(也可以hack别人的程序)

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define mod 1000000007
    using namespace std;
    int next[1000500],num[1000500],cases,l;
    char b[1000500];
    long long ans;
    long long fast_multi(long long m,long long n){
        long long jy=0;
        while(n){
            if(n&1)jy=(m+jy)%mod;
            m=(m+m)%mod;
            n>>=1;
        }
        return jy;
    }
    void get_next(){
        int j=0;num[1]=1;
        for(int i=2;b[i];i++){
            while(j&&b[i]!=b[j+1])j=next[j];
            if(b[i]==b[j+1])++j;
            next[i]=j;
            num[i]=num[j]+1;
        }
    }
    void get_sum(){
        get_next();
        int j=0;
        for(int i=2;b[i];i++){
            while(j&&b[i]!=b[j+1])j=next[j];
            if(b[i]==b[j+1])++j;
            while(j*2>i)j=next[j];
            ans=fast_multi(ans,num[j]+1)%mod;
        }
    }
    int main(){
        scanf("%d",&cases);
        while(cases--){
            ans=1;
            scanf("%s",b+1);
            get_sum();
            cout<<ans<<endl;
        }
    }

    这里写图片描述

  • 相关阅读:
    面向对象 & sql语句
    MySQL--数据库面试题汇集
    MySQL优化
    JAVA日报
    JAVA日报
    JAVA日报
    JAVA日报
    JAVA日报
    《大道至简》读后感
    JAVA日报
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532396.html
Copyright © 2011-2022 走看看