URAL1996 Cipher Message 3(KMP + FFT)

题目

Source

http://acm.timus.ru/problem.aspx?space=1&num=1996

Description

Emperor Palpatine has been ruling the Empire for 25 years and Darth Vader has been the head of the Empire Armed Forces. However, the Rebel movement is strong like it never used to be. One of the rebel leaders, Princess Leia from Alderaan, managed to get hold of secret blueprints of the Death Star, the imperial war station.
The Princess was going to deliver the station plan to the secret base for further analysis and searching for vulnerable spots. But her ship was attacked by the space destroyer "Devastator" headed by Darth Vader. At the last moment Princess Leia managed to send her findings to one of the closest planet called Tatooine with her droid R2-D2. Quite conveniently, an old friend of her father Obi-Wan Kenobi lives on that planet.
R2-D2 realizes the importance of his mission. He is going to encrypt the information so that the wrong people won’t get it.
The memory of R2-D2 has many files with images. First he wanted to use a well-known encrypting algorithm. The point of the method is to replace the least significant bits of the image with the encrypted message bits. The difference is practically unnoticeable on the picture, so one won’t suspect that it contains a hidden message.
But then R2-D2 decided that this method is quite well-known and the information won’t be protected enough. He decided to change the least significant bits of the image so that the secret information was a continuous sequence of the bytes of the image file. Help the droid determine if it is possible. And if it is, find the minimum number of bits to alter.

Input

The first line of the input contains integers n and m (1 ≤ n, m ≤ 250 000) — the sizes of the image file and of the file with the secret information in bytes. On the second line the content of the file with an image is given and the third line contains the secret information. The files are given as a sequence of space-separated bytes. Each byte is written as a sequence of eight bits in the order from the most to the least significant bit.

Output

Print "No", if it is impossible to encrypt information in this image. Otherwise, print in the first line "Yes", and in the second line — the number of bits to alter and the number of the byte in the file with the image, starting from which the secret information will be recorded. If there are multiple possible variants, print the one where the secret information is written closer to the beginning of the image file.

Sample Input

3 2
11110001 11110001 11110000
11110000 11110000

3 1
11110000 11110001 11110000
11110000

Sample Output

Yes
1 2

 

Yes
0 1

分析

题目看不懂说什么= =。。反正就是说给两个由8个01串组合成的序列A和B,现在能通过修改A序列中各01串的最后一位使得B串在A中匹配,问最少要修改多少位,且最开始匹配的位置是什么。

先不考虑最少修改几位。只考虑每个01串前面7位的话,B串在A串中所有能匹配的位置可以用KMP求出。

那么对于每一个匹配,怎么求出要修改几位使得8位都一样?可以构造多项式用FFT求出各个字符分别在主串各个位置中的子串和模式串的Hamming距离,这算FFT的一个经典应用吧,LA4671。。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 555555
const double PI=acos(-1.0);
 
struct Complex{
	double real,imag;
	Complex(double _real,double _imag):real(_real),imag(_imag){}
	Complex(){}
	Complex operator+(const Complex &cp) const{
		return Complex(real+cp.real,imag+cp.imag);
	}
	Complex operator-(const Complex &cp) const{
		return Complex(real-cp.real,imag-cp.imag);
	}
	Complex operator*(const Complex &cp) const{
		return Complex(real*cp.real-imag*cp.imag,real*cp.imag+cp.real*imag);
	}
	void setValue(double _real=0,double _imag=0){
		real=_real; imag=_imag;
	}
};
 
int len;
Complex wn[MAXN],wn_anti[MAXN];
 
void FFT(Complex y[],int op){
	for(int i=1,j=len>>1,k; i<len-1; ++i){
		if(i<j) swap(y[i],y[j]);
		k=len>>1;
		while(j>=k){
			j-=k;
			k>>=1;
		}
		if(j<k) j+=k;
	}
	for(int h=2; h<=len; h<<=1){
		Complex Wn=(op==1?wn[h]:wn_anti[h]);
		for(int i=0; i<len; i+=h){
			Complex W(1,0);
			for(int j=i; j<i+(h>>1); ++j){
				Complex u=y[j],t=W*y[j+(h>>1)];
				y[j]=u+t;
				y[j+(h>>1)]=u-t;
				W=W*Wn;
			}
		}
	}
	if(op==-1){
		for(int i=0; i<len; ++i) y[i].real/=len;
	}
}
void Convolution(Complex A[],Complex B[],int n){
	for(len=1; len<(n<<1); len<<=1);
	for(int i=n; i<len; ++i){
		A[i].setValue();
		B[i].setValue();
	}
	
	FFT(A,1); FFT(B,1);
	for(int i=0; i<len; ++i){
		A[i]=A[i]*B[i];
	}
	FFT(A,-1);
}

int cnt[MAXN];

int S[255555],T[255555],sn,tn;
int nxt[255555];

void get_nxt(int T[],int n){
	nxt[1]=0;
	int j=0;
	for(int i=2; i<=n; ++i){
		while(j>0 && T[j+1]!=T[i]) j=nxt[j];
		if(T[j+1]==T[i]) ++j;
		nxt[i]=j;
	}
}
int ansx=INF,ansy;
void KMP(int S[],int T[],int n,int m){
	int j=0;
	for(int i=1; i<=n; ++i){
		while(j>0 && T[j+1]!=S[i]) j=nxt[j];
		if(T[j+1]==S[i]) ++j;
		if(j==m){
			if(ansx>m-cnt[i-1]){
				ansx=m-cnt[i-1];
				ansy=i-m+1;
			}
			j=nxt[j];
		}
	}
}

int x[255555],y[255555];
Complex A[MAXN],B[MAXN];

int main(){
	for(int i=0; i<MAXN; ++i){
		wn[i].setValue(cos(2.0*PI/i),sin(2.0*PI/i));
		wn_anti[i].setValue(wn[i].real,-wn[i].imag);
	}
	int n,m,a;
	while(~scanf("%d%d",&n,&m)){
		for(int i=1; i<=n; ++i){
			scanf("%d",&a);
			x[i-1]=a&1;
			S[i]=a/10;
		}
		for(int i=1; i<=m; ++i){
			scanf("%d",&a);
			y[i-1]=a&1;
			T[i]=a/10;
		}
		
		if(m>n){
			puts("No");
			return 0;
		}
		
		for(int i=0; i<n; ++i) A[i].setValue(x[i]);
		for(int i=0; i<m; ++i) B[i].setValue(y[m-i-1]);
		for(int i=m; i<n; ++i) B[i].setValue();
		Convolution(A,B,n);
		for(int i=0; i<len; ++i){
			cnt[i]=(int)(A[i].real+0.5);
		}
		for(int i=0; i<n; ++i) A[i].setValue(!x[i]);
		for(int i=0; i<m; ++i) B[i].setValue(!y[m-i-1]);
		for(int i=m; i<n; ++i) B[i].setValue();
		Convolution(A,B,n);
		for(int i=0; i<len; ++i){
			cnt[i]+=(int)(A[i].real+0.5);
		}
		
		get_nxt(T,m);
		KMP(S,T,n,m);
		if(ansx==INF){
			puts("No");
			return 0;
		}
		puts("Yes");
		printf("%d %d
",ansx,ansy);	
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WABoss/p/5844900.html