zoukankan      html  css  js  c++  java
  • 【弱省胡策】Round #6 String 解题报告

    感觉这个题好神啊。

    首先我们只管 $a = b$ 的情况,那么我们自然就可以把这个串对 $a$ 取模,然后用 KMP 求出能弄出几个其他的 B 串。

    具体就是把串先倍长,然后倒过来,然后求 $Next$ 数组,然后从 $2n$ 开始沿着 $Next[]$ 跳,直到跳到 $le n$ 的时候停止,看哪些位置被跳到了,哪些位置就是合法的。

    问题是现在 $a eq b$ 怎么办。。?

    我猜啊,我们可以求出限制是 $a$ 的倍数时,哪些 B 串是合法的,再求出限制是 $b$ 的倍数是,哪些是合法的。

    然后只要在一种情况下合法,那么这个 B 串就可以写进来。

    至于为什么,我也不知道。反正和暴力拍了拍一直没出错。

    然后就做完啦~~~

    时间空间复杂度均为 $O(n)$。

     1 #include <cstdio>
     2 using namespace std;
     3 #define N 2000000 + 5
     4 
     5 int n, a, b, A[N], _A[N], Next[N];
     6 bool Flag[N];
     7 
     8 inline int getint()
     9 {
    10     char ch = '
    ';
    11     for (; ch != '-' && (ch > '9' || ch < '0'); ch = getchar()) ;
    12     int f = ch == '-' ? -1 : 1;
    13     int res = ch == '-' ? 0 : ch - '0';
    14     for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    15         res = (res << 3) + (res << 1) + ch - '0';
    16     return res * f;
    17 }
    18 
    19 inline void Work(int x)
    20 {
    21     for (int i = 1; i <= n; i ++)
    22         _A[i] = A[n - i + 1] % x;
    23     for (int i = 1; i <= n; i ++)
    24         _A[i + n] = _A[i];
    25     int k = 0, j = 1;
    26     Next[1] = 0;
    27     while (j <= (n << 1))
    28     {
    29         if (!k || _A[k] == _A[j])
    30             Next[++ j] = ++ k;
    31         else k = Next[k];
    32     }
    33     for (int x = n << 1 | 1; x > n + 1; x = Next[x])
    34         Flag[(n + 1 << 1) - x] = 1;
    35 }
    36 
    37 inline void Solve()
    38 {
    39     n = getint(), a = getint(), b = getint();
    40     for (int i = 1; i <= n; i ++)
    41     {
    42         A[i] = getint();
    43         Flag[i] = 0;
    44     }
    45     Work(a), Work(b);
    46     int ans = 0;
    47     for (int i = 1; i <= n; i ++)
    48         if (Flag[i]) ans ++;
    49     if (ans == 1) ans = 0;
    50     printf("%d
    ", ans);
    51 }
    52 
    53 int main()
    54 {
    55     for (int _ = getint(); _; _ --)
    56         Solve();
    57     
    58     return 0;
    59 }
    String_Gromah
  • 相关阅读:
    Ambari源代码分析之总览
    最简单的修改HashMap value值的方法
    机器学习 Hidden Markov Models 1
    OpenCV坐标系与操作像素的四种方法
    OpenCV2.4.13+VS2012开发环境配置
    OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
    OpenCV——PS滤镜算法之 Ellipsoid (凸出)
    如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)
    伊斯坦布尔的流浪 (三)
    伊斯坦布尔的流浪 (一)
  • 原文地址:https://www.cnblogs.com/gromah/p/4586475.html
Copyright © 2011-2022 走看看