传送门

解题思路

  因为要完全匹配,所以前七位必须保证相同,那么就可以把前7位提出来做一遍\(kmp\)匹配,最后的答案一定在这些位置里。考虑最后一位,可以把最后一位单独取出来,要计算的是最后一位相同的个数,那么就可以做两次\(fft\)得到\(haming dis\)。先把\(b\)翻转,然后做一次,得到的是全为\(1\)的个数,再把\(a,b\)取反做一次,得到的是全为\(0\)的个数,然后扫一遍\(kmp\)后的匹配位置,取个最小值。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib> using namespace std;
const int MAXN = 1000005;
const double Pi = acos(-1); int a[MAXN],b[MAXN],pos[MAXN],p[MAXN],rev[MAXN],id,n,m;
int nxt[MAXN],cnt,limit=1,c[MAXN],d[MAXN],ans=1e9;
struct Complex{
double x,y;
Complex(double _x=0,double _y=0){x=_x;y=_y;}
}f[MAXN],g[MAXN]; Complex operator+(Complex A,Complex B){return Complex(A.x+B.x,A.y+B.y);}
Complex operator-(Complex A,Complex B){return Complex(A.x-B.x,A.y-B.y);}
Complex operator*(Complex A,Complex B){return Complex(A.x*B.x-A.y*B.y,A.x*B.y+A.y*B.x);} void fft(Complex *f,int type){
for(int i=0;i<limit;i++) if(i<rev[i]) swap(f[i],f[rev[i]]);
Complex Wn,w,tmp;int len;
for(int i=2;i<=limit;i<<=1){
len=i>>1;Wn=Complex(cos(Pi/len),type*sin(Pi/len));
for(int j=0;j<limit;j+=i){
w=Complex(1,0);
for(int k=j;k<j+len;k++){
tmp=w*f[k+len];f[k+len]=f[k]-tmp;
f[k]=f[k]+tmp;w=w*Wn;
}
}
}
} inline void get_nxt(){
for(int i=2,j=0;i<=m;i++){
while(j>0 && b[i]!=b[j+1]) j=nxt[j];
if(b[i]==b[j+1]) j++;
nxt[i]=j;
}
} inline void get_match(){
for(int i=1,j=0;i<=n;i++){
while(j>0 && a[i]!=b[j+1]) j=nxt[j];
if(a[i]==b[j+1]) j++;
if(j==m) pos[++cnt]=i-m+1,j=nxt[j];
}
} inline void get_init(){
while(limit<=n+m) limit<<=1;
for(int i=0;i<limit;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?limit>>1:0);
} inline void get_ans(){
for(int i=0;i<n;i++) f[i].x=c[i],f[i].y=0;
for(int i=0;i<m;i++) g[i].x=d[i],g[i].y=0;
for(int i=n;i<limit;i++) f[i].x=f[i].y=0;
for(int i=m;i<limit;i++) g[i].x=g[i].y=0;
fft(f,1);fft(g,1);
for(int i=0;i<limit;i++) f[i]=f[i]*g[i];
fft(f,-1);
for(int i=0;i<=n-m;i++) p[i]+=(int)(f[i+m-1].x/limit+0.5);
} int main(){
scanf("%d%d",&n,&m);char s[10];
for(int i=1;i<=n;i++){
scanf("%s",s+1);
for(int j=1;j<=7;j++)
a[i]=(a[i]<<1)+(s[j]-'0');
c[i-1]=s[8]-'0';
}
for(int i=1;i<=m;i++){
scanf("%s",s+1);
for(int j=1;j<=7;j++)
b[i]=(b[i]<<1)+(s[j]-'0');
d[m-i]=s[8]-'0';
}
get_nxt();get_match();
if(!cnt) puts("No");
else{
puts("Yes");
get_init();get_ans();
for(int i=0;i<n;i++) c[i]^=1;
for(int i=0;i<m;i++) d[i]^=1;
get_ans();
for(int i=1;i<=cnt;i++)
if(m-p[pos[i]-1]<ans) {
ans=m-p[pos[i]-1];
id=pos[i];
}
printf("%d %d",ans,id);
}
return 0;
}

URAL 1996. Cipher Message 3(KMP+fft)的更多相关文章

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

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

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

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

  3. URAL 1996 Cipher Message 3

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

  4. URAL 1654 Cipher Message 解题报告

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

  5. ural Cipher Message

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

  6. URAL1996 Cipher Message 3(KMP + FFT)

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

  7. Gym 100285G Cipher Message 3

    题意 给\(N,M(N,M \le 250000)\)的两个由8位二进制表示的两个序列,允许改变每个数字的第8位的数值(即0→1,1→0),求改变最少次数使得长为\(M\)的序列为长为\(N\)的连续 ...

  8. hdu 4300 Clairewd’s message(扩展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

  9. hdu4300 Clairewd’s message 扩展KMP

    Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...

随机推荐

  1. 阿里巴巴下一代云分析型数据库AnalyticDB入选Forrester Wave™ 云数仓评估报告 解读

    前言近期, 全球权威IT咨询机构Forrester发布"The Forrester WaveTM: CloudData Warehouse Q4 2018"研究报告,阿里巴巴分析型 ...

  2. c#获取MAC地址和IP地址

    一获取mac地址 1.先添加system.management的dll组件2.添加引用 public string GetMACAddress(){string MoAddress = "& ...

  3. <自动化测试>之<unittest框架使用1>

    要说单元测试和UI自动化之间的是什么样的一个关系,说说我个人的一些心得体会吧,我并没有太多的这方面经验,由于工作本身就用的少,还有就是功能测试点点对于我这种比较懒惰的人来说,比单元测试复杂...思考单 ...

  4. Navicat12破解教程

    Navicat12破解教程 1.下载Navicat12 并安装,打开Navicat12 点击14天试用,关闭软件 2.下载注册机: 个人百度网盘(版本更新可能不及时):https://pan.baid ...

  5. MySQL replace 和 replace into 的用法

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...

  6. quartz的初步总结及配置优化

    1.scheduler 1. Scheduler就是Quartz的大脑,所有任务都是由它来设施.Scheduler包含一个两个重要组件: JobStore和ThreadPool.JobStore是会来 ...

  7. 力扣算法——136SingleNumber【E】

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  8. MySQL总结02

    sql优化 Insert使用批量 查询不要使用*, MySQ需要先查出表里的所有字段,再进行匹配 字符串的查询条件要带引号,否则可能不走索引 备份及恢复 备份 mysqldump -uroot -pp ...

  9. sass揭秘之@mixin,%,@function scss基本使用及操作函数

    sass揭秘之@mixin,%,@function: 地址:https://www.w3cplus.com/preprocessor/sass-mixins-function-placeholder. ...

  10. 【Movie】绿皮书

    今天和室友一起去看了<绿皮书>,一部获得奥斯卡的电影. 起初我是没什么太大期望的,纯粹是因为特价票10块钱,加上身边一个小姐姐的力荐. 看完我觉得,啊不愧是奥斯卡电影啊.推荐. 以下可能会 ...