• POJ2406 字符串的最小循环节 字符串哈希


    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #define pb push_back
    #define fi first
    #define se second
    #define io std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> pii;
    //const double pi=acos(-1);
    const ll p = 998244353, INF = 0x3f3f3f3f;
    const ll mod=1e9+7;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll exgcd(ll a, ll b, ll &x, ll &y) {if(!b) {x = 1; y = 0; return a;}ll r = exgcd(b, a % b, x, y);ll tmp = x; x = y, y = tmp - a / b * y;return r;}
    ll qpow(ll a,ll n,ll modd){ll r=1%mod;for (a%=modd; n; a=a*a%modd,n>>=1)if(n&1)r=r*a%modd;return r;}
    const double eps=1e-5;
    const ll maxn=1e6+10;
    ll lcm(ll a,ll b){return a*b/gcd(a,b);}
    char a[maxn];
    long long h[maxn];
    long long po[maxn];
    long long geth(int l,int r)
    {
       return ((h[r]-h[l-1]*po[r-l+1])%mod+mod)%mod;
    }
    int main()
    {
    
      while(~scanf("%s",a+1))
      {
          if(a[1]=='.')
            break;
          int n=strlen(a+1);
          po[0]=1;
          for(int i=1;i<=n;i++)
          {
              h[i]=(h[i-1]*p+a[i])%mod;
              po[i]=po[i-1]*p%mod;
          }
          for(int i=1;i<=n;i++)
          {    ll v=geth(1,i);
              int flag=1;
              if(n%i)
                continue;
              for(int l=i+1;l<=n;l+=i)
              {
                  int r=l+i-1;
                  if(r>n)
                  {
                      flag=0;
                      break;
                  }
                  if(geth(l,r)!=v)
                    {   flag=0;
                        break;
                    }
              }
              if(flag)
              {
                  printf("%d
    ",n/i);
                  break;
              }
          }
      }
    }
  • 相关阅读:
    Node的Buffer
    node中定时器的“先进”用法
    比较setImmediate(func),setTimeout(func),process.nextTick(func)
    node.js事件触发
    node.js express的安装过程
    ”靠谱的C#“单例模式
    JS性能消耗在哪里?
    如何建立索引
    优化之sitemap+RSS
    优化のzencart URL &zenid=.....
  • 原文地址:https://www.cnblogs.com/acmLLF/p/14642408.html
Copyright © 2020-2023  润新知