题目

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。。

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. #define INF (1<<30)
  7. #define MAXN 555555
  8. const double PI=acos(-1.0);
  9.  
  10. struct Complex{
  11. double real,imag;
  12. Complex(double _real,double _imag):real(_real),imag(_imag){}
  13. Complex(){}
  14. Complex operator+(const Complex &cp) const{
  15. return Complex(real+cp.real,imag+cp.imag);
  16. }
  17. Complex operator-(const Complex &cp) const{
  18. return Complex(real-cp.real,imag-cp.imag);
  19. }
  20. Complex operator*(const Complex &cp) const{
  21. return Complex(real*cp.real-imag*cp.imag,real*cp.imag+cp.real*imag);
  22. }
  23. void setValue(double _real=0,double _imag=0){
  24. real=_real; imag=_imag;
  25. }
  26. };
  27.  
  28. int len;
  29. Complex wn[MAXN],wn_anti[MAXN];
  30.  
  31. void FFT(Complex y[],int op){
  32. for(int i=1,j=len>>1,k; i<len-1; ++i){
  33. if(i<j) swap(y[i],y[j]);
  34. k=len>>1;
  35. while(j>=k){
  36. j-=k;
  37. k>>=1;
  38. }
  39. if(j<k) j+=k;
  40. }
  41. for(int h=2; h<=len; h<<=1){
  42. Complex Wn=(op==1?wn[h]:wn_anti[h]);
  43. for(int i=0; i<len; i+=h){
  44. Complex W(1,0);
  45. for(int j=i; j<i+(h>>1); ++j){
  46. Complex u=y[j],t=W*y[j+(h>>1)];
  47. y[j]=u+t;
  48. y[j+(h>>1)]=u-t;
  49. W=W*Wn;
  50. }
  51. }
  52. }
  53. if(op==-1){
  54. for(int i=0; i<len; ++i) y[i].real/=len;
  55. }
  56. }
  57. void Convolution(Complex A[],Complex B[],int n){
  58. for(len=1; len<(n<<1); len<<=1);
  59. for(int i=n; i<len; ++i){
  60. A[i].setValue();
  61. B[i].setValue();
  62. }
  63.  
  64. FFT(A,1); FFT(B,1);
  65. for(int i=0; i<len; ++i){
  66. A[i]=A[i]*B[i];
  67. }
  68. FFT(A,-1);
  69. }
  70.  
  71. int cnt[MAXN];
  72.  
  73. int S[255555],T[255555],sn,tn;
  74. int nxt[255555];
  75.  
  76. void get_nxt(int T[],int n){
  77. nxt[1]=0;
  78. int j=0;
  79. for(int i=2; i<=n; ++i){
  80. while(j>0 && T[j+1]!=T[i]) j=nxt[j];
  81. if(T[j+1]==T[i]) ++j;
  82. nxt[i]=j;
  83. }
  84. }
  85. int ansx=INF,ansy;
  86. void KMP(int S[],int T[],int n,int m){
  87. int j=0;
  88. for(int i=1; i<=n; ++i){
  89. while(j>0 && T[j+1]!=S[i]) j=nxt[j];
  90. if(T[j+1]==S[i]) ++j;
  91. if(j==m){
  92. if(ansx>m-cnt[i-1]){
  93. ansx=m-cnt[i-1];
  94. ansy=i-m+1;
  95. }
  96. j=nxt[j];
  97. }
  98. }
  99. }
  100.  
  101. int x[255555],y[255555];
  102. Complex A[MAXN],B[MAXN];
  103.  
  104. int main(){
  105. for(int i=0; i<MAXN; ++i){
  106. wn[i].setValue(cos(2.0*PI/i),sin(2.0*PI/i));
  107. wn_anti[i].setValue(wn[i].real,-wn[i].imag);
  108. }
  109. int n,m,a;
  110. while(~scanf("%d%d",&n,&m)){
  111. for(int i=1; i<=n; ++i){
  112. scanf("%d",&a);
  113. x[i-1]=a&1;
  114. S[i]=a/10;
  115. }
  116. for(int i=1; i<=m; ++i){
  117. scanf("%d",&a);
  118. y[i-1]=a&1;
  119. T[i]=a/10;
  120. }
  121.  
  122. if(m>n){
  123. puts("No");
  124. return 0;
  125. }
  126.  
  127. for(int i=0; i<n; ++i) A[i].setValue(x[i]);
  128. for(int i=0; i<m; ++i) B[i].setValue(y[m-i-1]);
  129. for(int i=m; i<n; ++i) B[i].setValue();
  130. Convolution(A,B,n);
  131. for(int i=0; i<len; ++i){
  132. cnt[i]=(int)(A[i].real+0.5);
  133. }
  134. for(int i=0; i<n; ++i) A[i].setValue(!x[i]);
  135. for(int i=0; i<m; ++i) B[i].setValue(!y[m-i-1]);
  136. for(int i=m; i<n; ++i) B[i].setValue();
  137. Convolution(A,B,n);
  138. for(int i=0; i<len; ++i){
  139. cnt[i]+=(int)(A[i].real+0.5);
  140. }
  141.  
  142. get_nxt(T,m);
  143. KMP(S,T,n,m);
  144. if(ansx==INF){
  145. puts("No");
  146. return 0;
  147. }
  148. puts("Yes");
  149. printf("%d %d\n",ansx,ansy);
  150. }
  151. return 0;
  152. }

URAL1996 Cipher Message 3(KMP + FFT)的更多相关文章

  1. Luogu 3375 【模板】KMP字符串匹配(KMP算法)

    Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...

  2. 【BZOJ3670】动物园(KMP算法)

    [BZOJ3670]动物园(KMP算法) 题面 BZOJ 题解 神TM阅读理解题 看完题目之后 想暴力: 搞个倍增数组来跳\(next\) 每次暴跳\(next\) 复杂度\(O(Tnlogn)\) ...

  3. 【BZOJ3670】【NOI2014】动物园(KMP算法)

    [BZOJ3670]动物园(KMP算法) 题面 BZOJ 题解 神TM阅读理解题 看完题目之后 想暴力: 搞个倍增数组来跳\(next\) 每次暴跳\(next\) 复杂度\(O(Tnlogn)\) ...

  4. 稀疏傅里叶变换(sparse FFT)

    作者:桂. 时间:2018-01-06  14:00:25 链接:http://www.cnblogs.com/xingshansi/p/8214122.html 前言 对于数字接收来讲,射频域随着带 ...

  5. 【Luogu5349】幂(分治FFT)

    [Luogu5349]幂(分治FFT) 题面 洛谷 题解 把多项式每一项拆出来考虑,于是等价于要求的只有\(\sum_{i=0}^\infty i^kr^i\). 令\(f(r)=\sum_{i=0} ...

  6. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  7. (KMP 扩展)Clairewd’s message -- hdu -- 4300

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 Clairewd’s message Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU4609 3-idiots(母函数 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...

  9. hdu 4609 3-idiots(快速傅里叶FFT)

    比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...

随机推荐

  1. supersr--控制器的生命周期:

    调用顺序为 1 init函数(init;initWithFrame;initWithCoder;等)--初始化 2 awakeFromNib--在loadView之前的工作放在这里 3 viewDid ...

  2. NYOJ题目770仿射密码

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs4AAAIUCAIAAACFKz0yAAAgAElEQVR4nO3dPXLruLaG4TsJ5RqIYw

  3. C# 中的Singleton模式

    一般写Singleton基本都是一下这个套路 class Singleton { public static Singleton instance; private Singleton() { } p ...

  4. 关于python装饰器(Decorators)最底层理解的一句话

    一个decorator只是一个带有一个函数作为参数并返回一个替换函数的闭包. http://www.xxx.com/html/2016/pythonhexinbiancheng_0718/1044.h ...

  5. Tiny Rss Reader - 迷你RSS阅读器

    发布新软件 TinyRss: Windows平台上的一个小巧的Rss阅读器. 用户界面: 项目地址: https://github.com/movsb/tinyrss.git 测试下载: http:/ ...

  6. Golang gopath

    golang 的gopath 至关重要,会影响到我们import package. golang 支持以相对路径的方式import,但是这种方式是不推荐的. 推荐的做法是在gopath中添加我们的项目 ...

  7. android 入门-安装环境

    1.安装jdk 相关链接 2.安装adt 里面包含eclipse 3.下载androidsdk 4.打开eclipse 找到windows -> 属性 -> android 主目录 复制 ...

  8. PHPCMS 多站点管理切换问题

    打开系统函数库global.func.php 可以看到获取站点ID的函数如下 /** * 获取当前的站点ID */ function get_siteid() { static $siteid; if ...

  9. linux 操作mysql

    MySQL删除数据库时的错误 ERROR 1010 (HY000): Error dropping database (can't rmdir './myapp', errno: 39)的错误信息. ...

  10. oc精简笔记

    首先如果是想在终端学习的话,以下内容是必须的,如果是直接使用xcode的请随意: operating system      os       X ter   终端的缩写 ls      显示目录文件 ...