— 数论 — 2 min read
题目链接:click here
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.
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.
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.
input | output |
---|---|
3 2 11110001 11110001 11110000 11110000 11110000 | Yes 1 2 |
input | output |
---|---|
3 1 11110000 11110001 11110000 11110000 | Yes 0 1 |
Problem Author: Denis Dublennykh (prepared by Oleg Dolgorukov)
题目老长难懂,其实就是给你一个n byte的01A串,m byte的01B串.其中A串中每一byte的最后一个bit是可以修改的,问至少修改多少次A串能使B使A的子串。输出修改次数与最小的起始匹配位置。
首先将A串每byte取前7位(代码中的a数组)与B串每byte取前7位(代码中的b数组)用KMP匹配,因为前7位不能改,所以如果没有一处匹配就直接输出No了。
然后将A串每byte取后1位(代码中的ax数组)与B串每byte取后一位(代码中的bx数组)求hamming距离。
如何求ax与bx的hamming距离呢?
可以巧妙的ax作为卷积中的f函数,bx的逆向数组bx`作为卷积中的g函数。两者求卷积。这样就成了: $$c[i + m - 1] = ax[i + 0] bx`[m - 0 - 1] + ax[i + 1] bx`[m - 1 - 1] + …… ax[i + j] bx`[m - j - 1] + …… ax[i + m - 1] bx`[m - (m - 1) - 1]$$
可以看出,如果ax , bx中同为1,乘积为1,否则为0,这样就能统计出了有多少位同为1了。
如果将ax,bx数组中的01取反,再进行上述操作,那么救能统计出有多少位同为0了。两者相加就是全部相同的有多少,用总字符数m减去它,就能求出hamming距离了!
卷积复杂度为O(nlg(n))而暴力算法的复杂度为O(n×m),真是太神奇了!