Binary Witch
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1521 | Accepted: 558 |
Description
Once upon a time in the silent depths of digital forests there lived a Binary Witch. She was able to forecast weather, telling for any day in the future whether it will be rainy or sunny.
Her magic was based on the following ancient rule: let a1, a2, ..., aN be the sequence of binary digits, where ai = 0 indicates that i-th day was rainy, and ai = 1 -- that it was sunny. To predict the weather in day N+1, consider the t-postfix a N-t+1, a N-t+2, ..., aN consisting of the last t elements. If that postfix is encountered somewhere before the position N-t+1, i.e. if there is such k <= N-t, that ak = a N-t+1, a k+1 = a N-t+2, ..., a k+t-1 = aN then the predicted value will be a k+t.
If there is more than one occurrence of t-postfix, then the rightmost one (with maximal k) will be taken. So, to make a prediction, she tried t-postfixes, consequently for t = 13, 12, ..., 1, stopping after the first prediction. If neither postfix was found, she predicted rain ("0"). If prediction for more than one day is needed, it is assumed that all previous days are predicted correctly, so if first predicted value is b, then we make forecast for day N+2 based on N+1 values, where a N+1 = b.
Because the witch was burned long ago, your task is to write a program to perform her arcane job.
Her magic was based on the following ancient rule: let a1, a2, ..., aN be the sequence of binary digits, where ai = 0 indicates that i-th day was rainy, and ai = 1 -- that it was sunny. To predict the weather in day N+1, consider the t-postfix a N-t+1, a N-t+2, ..., aN consisting of the last t elements. If that postfix is encountered somewhere before the position N-t+1, i.e. if there is such k <= N-t, that ak = a N-t+1, a k+1 = a N-t+2, ..., a k+t-1 = aN then the predicted value will be a k+t.
If there is more than one occurrence of t-postfix, then the rightmost one (with maximal k) will be taken. So, to make a prediction, she tried t-postfixes, consequently for t = 13, 12, ..., 1, stopping after the first prediction. If neither postfix was found, she predicted rain ("0"). If prediction for more than one day is needed, it is assumed that all previous days are predicted correctly, so if first predicted value is b, then we make forecast for day N+2 based on N+1 values, where a N+1 = b.
Because the witch was burned long ago, your task is to write a program to perform her arcane job.
Input
First line of input file contains two integers N (1 <= N <= 1000000) and L (1 <= L <= 1000), separated by space. Second line contains a string of N characters "0" and "1".
Output
Output file must contain a single string of L characters, which are forecasts for days N+1, N+2, ..., N+L.
Sample Input
10 7 1101110010
Sample Output
0100100
solution:
给你一个长度为N的串S,找出S[k..k+t-1]=s[N-t+1..N],其中1<=t<=13,k<=N-t,有多个k满足时,选择t最大的并且最靠右的,令S[N+1]=S[k+t],如果找不到符合条件的的K,则S[N+1]='0'。
由于每个子串都由01组成,可以看作一个2进制数,因为t的范围<=13,所以用2^13就可以表示出所有的子串,用last[i][j]表示长度为i内容为j的子串的最晚结束位置。只要扫一遍数组就可以了,遇到长度为i内容为j的子串就可以刷新last[i][j]。这样在处理第i位的时候,last中就保存了从1~i-1为所有长度为1~13的子串的最后出现位置。复杂度是O((N+M)*13)。
#include <stdio.h>
#include <string.h>
int n, l;
char s[1002000];
int last[13][9000];
int main()
{
while (scanf("%d%d", &n, &l) != EOF)
{
scanf("%s", s);
memset(last, -1, sizeof(last));
for (int i = 0; i<n + l - 1; i++)
{
int now = 0, tmp = 1;
if (i >= n - 1)s[i + 1] = '0';
for (int j = 0; j<13 && j<=i; j++)
{
int x = s[i - j] - '0';
if (x)now += tmp;
tmp <<= 1;
if (i >= n - 1 && last[j][now] != -1)s[i + 1] = s[last[j][now] + 1];
last[j][now] = i;
}
}
s[n + l] = '\0';
printf("%s\n", s + n);
}
return 0;
}