1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <bits/stdc++.h>
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; }
template<class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = (x + (x << 2) << 1) + (c ^ '0'); if (iosig) x = -x; }
inline int read(char *buf) { register size_t s = 0; register char ch; while (ch = read(), isspace(ch) && ch != -1); if (ch == EOF) { *buf = '\0'; return -1; } do buf[s++] = ch; while (ch = read(), !isspace(ch) && ch != -1); buf[s] = '\0'; return s; }
const int OUT_LEN = 10000000; char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; }
template<class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) { print('0'); } else { if (x < 0) print('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) print((char)buf[cnt--]); } }
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
namespace FastFourierTansform {
const int MAXN = 300005;
struct Complex { double r, i;
Complex(double r = 0, double i = 0) : r(r), i(i) {}
inline Complex operator + (const Complex &x) { return Complex(r + x.r, i + x.i); }
inline Complex operator - (const Complex &x) { return Complex(r - x.r, i - x.i); }
inline Complex operator * (const Complex &x) { return Complex(r * x.r - i * x.i, r * x.i + i * x.r); }
inline Complex conj() { return Complex(r, -i); } } A[MAXN], C[MAXN];
const double PI = acos(-1);
inline void fft(Complex *a, const int n, const int f) { register int i, j, k; for (i = j = 0; i < n; i++) { if (i > j) std::swap(a[i], a[j]); for (k = n >> 1; (j ^= k) < k; k >>= 1); } for (i = 1; i < n; i <<= 1) { Complex wn(cos(PI / i), f * sin(PI / i)); for (j = 0; j < n; j += i << 1) { Complex w(1, 0); for (k = 0; k < i; k++, w = w * wn) { Complex x = a[j + k], y = w * a[i + j + k]; a[j + k] = x + y, a[i + j + k] = x - y; } } } if (f == -1) for (i = 0; i < n; i++) a[i].r /= n; }
char in[10]; int a[MAXN], b[MAXN], pre[MAXN], tot, cost[MAXN]; int pat[MAXN], tmp[MAXN], fail[MAXN], n, m;
inline int parse() { register int ret = 0; register char *p = in; ret = ((*p - 48) << 7) | ((*(p + 1)) - 48 << 6) | ((*(p + 2)) - 48 << 5) | ((*(p + 3)) - 48 << 4) | ((*(p + 4)) - 48 << 3) | ((*(p + 5)) - 48 << 2) | ((*(p + 6)) - 48 << 1) | ((*(p + 7)) - 48); return ret; }
inline void kmp() { fail[1] = fail[2] = 1; for (register int j = 1, i = 2; i <= m; i++) { while (j > 1 && tmp[i] != tmp[j]) j = fail[j]; fail[i + 1] = tmp[i] == tmp[j] ? ++j : j; } for (register int j = 1, i = 1; i <= n; i++) { while (j > 1 && tmp[j] != pat[i]) j = fail[j]; if (tmp[j] == pat[i]) j++; if (j > m) pre[++tot] = i; } }
int len;
inline void match(int p) { for (register int i = 0; i < n; i++) A[i].r = a[i + 1] & 1 ^ p; for (register int i = 0; i < m; i++) A[i].i = b[i + 1] & 1 ^ p ^ 1; for (register int i = n; i < len; i++) A[i].r = 0; for (register int i = m; i < len; i++) A[i].i = 0; fft(A, len, 1); for (register int i = 0, j; i <= len; i++) { j = (len - i) & (len - 1), C[i] = (A[i] * A[i] - (A[j] * A[j]).conj()) * Complex(0, -0.25); } fft(C, len, -1); for (register int i = 0; i <= len; i++) cost[i] += (int)(C[i].r + 0.5); }
inline void solve() { read(n), read(m); for (register int i = 1; i <= n; i++) { read(in), a[i] = parse(), pat[i] = a[i] & 1 ? a[i] : a[i] ^ 1; } for (register int i = 1; i <= m; i++) { read(in), b[i] = parse(), tmp[i] = b[i] & 1 ? b[i] : b[i] ^ 1; } kmp(); if (!tot) puts("No"), exit(0); puts("Yes"); for (len = 1; len <= n; len <<= 1); std::reverse(b + 1, b + m + 1); match(0), match(1); register int min = INT_MAX, id = 0; for (register int j = 1; j <= tot; j++) cost[pre[j] - 1] < min ? (min = cost[pre[j] - 1], id = pre[j] - m + 1) : 0; printf("%d %d\n", min, id); } }
int main() { FastFourierTansform::solve(); flush(); return 0; }
|