zoukankan      html  css  js  c++  java
  • [NOI2014]动物园

    嘟嘟嘟


    NOI2014水题第二道。


    起手暴力,神经兮兮的写了个(O(Tn ^ 2))的双哈希,只得了30分。(5e8开O2果然跑不过去)


    昨晚写的暴力,今天又看了看这道题,发现就是求一个点跳多少次fail指针能到头。这个求fail指针的时候顺带就能递推出来。然后直接暴力跳kmp的fail指针直到不大于(frac{i}{2})的位置就好了,好写还更快。


    但是如果数据全是'a',那上面的算法就是(O(n ^ 2))的了。
    怎么优化?无脑上倍增啊!
    然后就过了……
    辛亏自己代码习惯好,把倍增数组小的一维放在了前面,(O(Tnlogn))无压力AC。刚刚看yyb的题解也写的倍增然后因为大数组在第一维被卡成了80hhh。


    但正解是(O(n))的。我们在统计答案的时候再跑一遍kmp,如果fail指针跳完后比(frac{i}{2})大,就往回跳……好像也挺无脑的……


    放一个倍增版的吧。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<assert.h>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e6 + 5;
    const int N = 20;
    const ll mod = 1e9 + 7;
    In ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    In void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
      freopen("random.in", "r", stdin);
      freopen("ha.out", "w", stdout);
    #endif
    }
    
    int n;
    char s[maxn];
    
    int tot[maxn], f[maxn], fa[N + 2][maxn];
    In void init()
    {
      Mem(tot, 0), Mem(f, 0), Mem(fa, 0);
      tot[1] = 1, f[1] = 0;
      for(int i = 2, j = 0; i <= n; ++i)
        {
          while(j && s[j + 1] != s[i]) j = f[j];
          if(s[j + 1] == s[i]) ++j;
          f[i] = j, tot[i] = tot[f[i]] + 1;
        }
      for(int i = 1; i <= n; ++i) fa[0][i] = f[i];
      for(int j = 1; (1 << j) <= n; ++j)
        for(int i = 1; i <= n; ++i) fa[j][i] = fa[j - 1][fa[j - 1][i]];
    }
    
    In int jump(int x)
    {
      int y = x >> 1, pos = 0;
      for(int i = N; i >= 0; --i)
        {
          if(fa[i][x] <= y) pos = max(pos, fa[i][x]);
          if(fa[i][x] && fa[i][x] >= y)
    	{
    	  pos = max(pos, fa[i + 1][x]);
    	  x = fa[i][x];
    	}
        }
      return x <= y ? x : pos;
    }
    
    int main()
    {
      //MYFILE();
      int T = read();
      while(T--)
        {
          scanf("%s", s + 1);
          n = strlen(s + 1);
          init();
          ll ans = 1;
          for(int i = 1; i <= n; ++i) ans = ans * (tot[jump(i)] + 1) % mod;
          write(ans), enter;
        }
      return 0;
    }
    
  • 相关阅读:
    php7垃圾回收机制
    PHP-FPM
    go main.main之前的准备
    ElementUI MessageBox 弹框
    ElementUI table 样式修改
    ElementUI Dropdown 下拉菜单
    vue+django实现下载文件
    ElementUI 点击展开/隐藏
    vue+django实现websocket连接
    ElementUI 上传文件以及限制
  • 原文地址:https://www.cnblogs.com/mrclr/p/10919275.html
Copyright © 2011-2022 走看看