题意

给\(N,M(N,M \le 250000)\)的两个由8位二进制表示的两个序列,允许改变每个数字的第8位的数值(即0→1,1→0),求改变最少次数使得长为\(M\)的序列为长为\(N\)的连续子序列,在次数最少的前提下,找到下标最小的起始位置。

思路

  • 因为只能改变第8位的状态,所以若能匹配的话,前7位数值需要相同,这步通过KMP完成即可。
  • 剩下就是如何快速求每个匹配区间所需要的代价,即找出两个长为\(M\)的序列对应位不同的个数。
  • 考虑位置\(A[i, i + M - 1]\),若按\(0.. M-1\)对应匹配显然没什么规律,而如果按\(M-1..0\),则匹配位置的下标和均为\(i+M-1\),将0和1分开作,用FFT完成计数即可。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
#include<assert.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i,l,r) for(int i=(l);i<(r);++i)
#define setIO(x) freopen(x".in","r", stdin);freopen(x".out","w",stdout);
typedef long long ll;
typedef pair<int, int> pii;
const ll LINF = 1e10 + 7;
const int N = 3e5 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-8;
//-----------------head-----------------
struct C {
double r, i;
C() {
r = i = 0;
}
C(double _r, double _i) {
r = _r, i = _i;
}
C operator+(const C &p) const {
return C(r + p.r, i + p.i);
}
C operator-(const C &p) const {
return C(r - p.r, i - p.i);
}
C operator*(const C &p) const {
return C(r * p.r - i * p.i, r * p.i + i * p.r);
}
} A[N * 2], B[N * 2];
int n, m, a[N], b[N], p[N], s[2][N];
void fft(C x[], int n, int rev) {
int i, j, k, t;
for (i = 1; i < n; ++i) {
for (j = 0, k = n >> 1, t = i; k; k >>= 1, t >>= 1)
j = j << 1 | (t & 1);
if (i < j)
swap(x[i], x[j]);
}
for (int s = 2, ds = 1; s <= n; ds = s, s <<= 1) {
C w = C(1, 0), t;
C wn = C(cos(2.0 * rev * PI / s), sin(2.0 * rev * PI / s));
for (k = 0; k < ds; ++k, w = w * wn)
for (i = k; i < n; i += s) {
t = w * x[i + ds];
x[i + ds] = x[i] - t;
x[i] = x[i] + t;
}
}
if (rev == -1)
for (i = 0; i < n; ++i)
x[i].r /= n;
}
int read() {
int x = 0;
char ch = getchar();
while (ch != '0' && ch != '1')
ch = getchar();
while (ch == '0' || ch == '1') {
x = (x << 1) | (ch - '0');
ch = getchar();
}
return x;
}
void solve() {
int L = 1;
while (L < n + m)
L <<= 1;
rep(w, 0, 2)
{
rep(i, 0, n)
A[i] = C((a[i] & 1) == w, 0);
rep(i, n, L)
A[i] = C(0, 0);
rep(i, 0, m)
B[m - i - 1] = C((b[i] & 1) == w, 0);
rep(i, m, L)
B[i] = C(0, 0);
fft(A, L, 1), fft(B, L, 1);
rep(i, 0, L)
A[i] = A[i] * B[i];
fft(A, L, -1);
rep(i, 0, n)
s[w][i] = A[i].r + EPS;
}
rep(i, 0, n)
a[i] >>= 1;
rep(i, 0, m)
b[i] >>= 1;
p[0] = -1;
for (int i = 1, j = -1; i < m; ++i) {
while (j >= 0 && b[j + 1] != b[i])
j = p[j];
j += b[j + 1] == b[i];
p[i] = j;
}
pii ans = mp(INF, INF);
for (int i = 0, j = -1; i < n; ++i) {
while (j >= 0 && b[j + 1] != a[i])
j = p[j];
j += b[j + 1] == a[i];
if (j == m - 1) {
pii tmp = mp(m - s[0][i] - s[1][i], i - m + 2);
ans = min(ans, tmp);
j = p[j];
}
}
if (ans.first < INF) {
puts("Yes");
printf("%d %d\n", ans.first, ans.second);
} else {
puts("No");
}
}
int main() {
scanf("%d%d", &n, &m);
rep(i, 0, n)
a[i] = read();
rep(i, 0, m)
b[i] = read();
solve();
return 0;
}

Gym 100285G Cipher Message 3的更多相关文章

  1. ural Cipher Message

    Cipher Message Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  2. URAL1996 Cipher Message 3(KMP + FFT)

    题目 Source http://acm.timus.ru/problem.aspx?space=1&num=1996 Description Emperor Palpatine has be ...

  3. URAL 1654 Cipher Message 解题报告

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1654 题意:简单的理解就是,把一个序列中相邻的且是偶数个相同的字符删除,奇数个的话就只保 ...

  4. Cipher Message

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34121#problem/C // File Name: c.cpp // Author: ...

  5. URAL 1996 Cipher Message 3 (FFT + KMP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :给出两个串A , B,每个串是若干个byt ...

  6. URAL 1996 Cipher Message 3

    题目 神题. 记得当初DYF和HZA讲过一个FFT+KMP的题目,一直觉得很神,从来没去做. 没有真正理解FFT的卷积. 首先考虑暴力. 只考虑前7位 KMP 找出所有 B 串可以匹配 A 串的位置. ...

  7. URAL1966 Cipher Message 3

    题目描述 题解: 能看出来的是,每一组数只能改最后一位,所以前$7$位动不了. 所以$KMP$跑一跑. 重点在于最后一位怎么搞. 如果$KMP$跑完了还没找到合适的位置,直接$puts("N ...

  8. Ural 1996 Cipher Message 3 (生成函数+FFT)

    题面传送门 题目大意:给你两个$01$串$a$和$b$,每$8$个字符为$1$组,每组的最后一个字符可以在$01$之间转换,求$b$成为$a$的一个子串所需的最少转换次数,以及此时是从哪开始匹配的. ...

  9. URAL 1996. Cipher Message 3(KMP+fft)

    传送门 解题思路 因为要完全匹配,所以前七位必须保证相同,那么就可以把前7位提出来做一遍\(kmp\)匹配,最后的答案一定在这些位置里.考虑最后一位,可以把最后一位单独取出来,要计算的是最后一位相同的 ...

随机推荐

  1. powershell 判断操作系统版本 命令

    powershell 传教士 原创文章.始于 2015-12-15 允许转载,但必须保留名字和出处,否则追究法律责任 一 前言 判断操作系统版本,是个老话题,bat.vbs中都有例子,这本不是重要问题 ...

  2. win10系统的点评

    Windows 10 是美国微软公司所研发的新一代跨平台及设备应用的操作系统.在正式版本发布一年内,所有符合条件的Windows7.Windows 8.1的用户都将可以免费升级到Windows 10, ...

  3. [windows操作系统]windows模块

    smss.exe csrss.exe    Client/Server Runtime Server Subsystem

  4. Note_Master-Detail Application(iOS template)_02_YJYAppDelegate.m

    //YJYAppDelegate.m #import "YJYAppDelegate.h" #import "YJYMasterViewController.h" ...

  5. Java中的blank final

    Java allows the creation of blank finals, which are fields that are declared as final but are not gi ...

  6. 需求分析(NABC)

    团队开发需求分析 队长:郭庆樑 成员:林彦汝.张金 经过讨论,我们决定做一个基于Windows的小游戏——躲避小球. 把这个项目实现,组长强调有两点: 1.可实现:2.有用户. 可以说,我们最大的特点 ...

  7. iOS多线程之NSOperation,NSOperationQueue

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  8. HTTP协议详解(经典)

    转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的 ...

  9. BZOJ 3439 Kpm的MC密码

    倒着建trie,然后主席树来求子树第k大. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  10. ACDream-C - Transformers' Mission(Dijastra最短路径)

    dijstra求最短路径:经典应用题目: 题意:给你一个带权值无向图,权值是A点到B点的时间,然后告诉你起点,一个人可以去炸掉一个结点或多个节点,也可以派多个人,最终这些人在终点集合,问最后一个到达终 ...