URAL1966 Cipher Message 3
题解:
能看出来的是,每一组数只能改最后一位,所以前$7$位动不了。
所以$KMP$跑一跑。
重点在于最后一位怎么搞。
如果$KMP$跑完了还没找到合适的位置,直接$puts("No")$就好了。
剩下个匹配问题。
(要不是数据范围拦着我我都想建图跑费用流了)
这个匹配可以用$FFT$求。
$FFT$不是求$\sum(a[i]*b[j-i])$的吗?怎么求字符串匹配?
其实我们只想要最后多项式中某一位上的值,所以$b[j-i]$和$b[i]$没有区别,反转数组就好了。
我们要求的是需要多少个$0$变成$1$,以及多少个$1$变成$0$。
所以$FFT$求两遍卷积,第一次$a[i]=[s1[i]==0],b[i]=[s2[i]==1]$,注意这里的$b$还没有反转。
第二次反过来就可以了。
代码:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int N = ;
const ull seed = ;
const double Pi = acos(-1.0);
template<typename T>
inline void read(T&x)
{
T f = ,c = ;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){c=c*+ch-'';ch=getchar();}
x = f*c;
}
struct cp
{
double x,y;
cp(){}
cp(double x,double y):x(x),y(y){}
cp operator + (const cp&a)const{return cp(x+a.x,y+a.y);}
cp operator - (const cp&a)const{return cp(x-a.x,y-a.y);}
cp operator * (const cp&a)const{return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
};
int to[*N],lim=,l;
void fft(cp *a,int len,int k)
{
for(int i=;i<len;i++)
if(i<to[i])swap(a[i],a[to[i]]);
for(int i=;i<len;i<<=)
{
cp w0(cos(Pi/i),k*sin(Pi/i));
for(int j=;j<len;j+=(i<<))
{
cp w(,);
for(int o=;o<i;o++,w=w*w0)
{
cp w1 = a[j+o],w2 = a[j+o+i]*w;
a[j+o] = w1+w2;
a[j+o+i] = w1-w2;
}
}
}
if(k==-)
for(int i=;i<len;i++)
a[i].x/=len;
}
void rd(int n,int *a1,int *a2)
{
for(int x,i=;i<=n;i++)
{
read(x);
a1[i] = x%;
a2[i] = x/;
}
}
int n,m,a[][N],b[][N];
bool vis[N];
int ans[N];
cp c1[*N],c2[*N],c3[*N];
int nxt[N];
void get_nxt(int *a,int len)
{
int i = ,j = ;
while(i<=len)
{
if(!j||a[i]==a[j])
{
i++,j++;
nxt[i] = j;
}else j = nxt[j];
}
}
int kmp(int *a,int la,int *b,int lb)
{
int ret = ;
int i = ,j = ;
while(i<=la)
{
if(a[i]==b[j]||j==)
{
i++,j++;
if(j==lb+)
{
vis[i-]=;
j = nxt[j];
ret = ;
}
}else j=nxt[j];
}
return ret;
}
void work()
{
fft(c1,lim,),fft(c2,lim,);
for(int i=;i<lim;i++)c3[i]=c1[i]*c2[i];
fft(c3,lim,-);
}
void init()
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(ans,,sizeof(ans));
memset(vis,,sizeof(vis));
lim=,l=;
}
int main()
{
// freopen("tt.in","r",stdin);
while(scanf("%d%d",&n,&m)>)
{
init();
rd(n,a[],a[]),rd(m,b[],b[]);
get_nxt(b[],m);
if(!kmp(a[],n,b[],m))
{
puts("No");
continue;
}
puts("Yes");
while(lim<*(n+m))lim<<=,l++;
for(int i=;i<lim;i++)to[i]=((to[i>>]>>)|((i&)<<(l-)));
for(int i=;i<lim;i++)c1[i]=c2[i]=cp(,);
for(int i=;i<n;i++)c1[i].x=(a[][i+]==);
for(int i=;i<m;i++)c2[i].x=(b[][m-i]==);
work();
for(int i=;i<n;i++)ans[i+]=(int)(c3[i].x+0.5);
for(int i=;i<lim;i++)c1[i]=c2[i]=cp(,);
for(int i=;i<n;i++)c1[i].x=(a[][i+]==);
for(int i=;i<m;i++)c2[i].x=(b[][m-i]==);
work();
for(int i=;i<n;i++)ans[i+]+=(int)(c3[i].x+0.5);
int mn = N,pos=;
for(int i=m;i<=n;i++)
if(vis[i]&&ans[i]<mn)
{
mn = ans[i];
pos = i-m+;
}
printf("%d %d\n",mn,pos);
}
return ;
}
URAL1966 Cipher Message 3的更多相关文章
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- URAL1996 Cipher Message 3(KMP + FFT)
题目 Source http://acm.timus.ru/problem.aspx?space=1&num=1996 Description Emperor Palpatine has be ...
- URAL 1654 Cipher Message 解题报告
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1654 题意:简单的理解就是,把一个序列中相邻的且是偶数个相同的字符删除,奇数个的话就只保 ...
- Gym 100285G Cipher Message 3
题意 给\(N,M(N,M \le 250000)\)的两个由8位二进制表示的两个序列,允许改变每个数字的第8位的数值(即0→1,1→0),求改变最少次数使得长为\(M\)的序列为长为\(N\)的连续 ...
- Cipher Message
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34121#problem/C // File Name: c.cpp // Author: ...
- URAL 1996 Cipher Message 3 (FFT + KMP)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :给出两个串A , B,每个串是若干个byt ...
- URAL 1996 Cipher Message 3
题目 神题. 记得当初DYF和HZA讲过一个FFT+KMP的题目,一直觉得很神,从来没去做. 没有真正理解FFT的卷积. 首先考虑暴力. 只考虑前7位 KMP 找出所有 B 串可以匹配 A 串的位置. ...
- Ural 1996 Cipher Message 3 (生成函数+FFT)
题面传送门 题目大意:给你两个$01$串$a$和$b$,每$8$个字符为$1$组,每组的最后一个字符可以在$01$之间转换,求$b$成为$a$的一个子串所需的最少转换次数,以及此时是从哪开始匹配的. ...
- URAL 1996. Cipher Message 3(KMP+fft)
传送门 解题思路 因为要完全匹配,所以前七位必须保证相同,那么就可以把前7位提出来做一遍\(kmp\)匹配,最后的答案一定在这些位置里.考虑最后一位,可以把最后一位单独取出来,要计算的是最后一位相同的 ...
随机推荐
- E20170414-ms
collapse v/n 奔溃,垮台 constraint n 约束,限制 adaptive adj 适应的; 有适应能力的; exhausitive adj. 详尽的; store n 商店,仓 ...
- 纯拓扑排序一搞poj2367
/* author: keyboarder time : 2016-05-18 12:21:26 */ #include<cstdio> #include<string.h> ...
- U3D 的一些基础优化
1.在使用数组或ArrayList对象时应当注意 [csharp] view plaincopy length=myArray.Length; for(int i=0;i<length;i++) ...
- bzoj 4316: 小C的独立集【仙人掌dp】
参考:https://www.cnblogs.com/clrs97/p/7518696.html 其实和圆方树没什么关系 设f[i][j][k]为i点选/不选,这个环的底选不选 这个底的定义是设u为这 ...
- hdu 3038 How Many Answers Are Wrong【带权并查集】
带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...
- 前端代码规范(转载 http://codeguide.bootcss.com/)
http://codeguide.bootcss.com/ HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 J ...
- PJzhang:漏洞渗透测试框架“天使之剑(AngelSword)”
猫宁!!! 参考链接: www.phpinfo.cc/?post=42 https://www.freebuf.com/sectool/149883.html 同事介绍了一款渗透测试框架AngelSw ...
- Ubuntu 18.04 LTS 安装后 各种问题以及解决方案
1. root的初始密码,默认是不知道的,需要进行设置 a. 进入终端自己的用户 b. 输入 sudo passwd回车 c. 输入新密码,回车,重复,回车,搞定 d. su 一下,就可以了 2. ...
- css为什么要定最小宽度和最大宽度,最小宽度和最大宽度有什么用
设最小和最大宽度,主要是为了防止页面变形而已如,如果一个页面的宽度设置为百分比,这时此页面的宽度会根据浏览器的宽度而定但如果浏览器的宽度过小,页面就会变形,例如,你做的用百分比设置的页面,而用户端的浏 ...
- D Tree HDU - 4812
https://vjudge.net/problem/HDU-4812 点分就没一道不卡常的? 卡常记录: 1.求逆元忘开longlong 2.把solve中分离各个子树的方法,由“一开始全部加入,处 ...