POJ 2541 Binary Witch

  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.

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
 
  
解题思路:
因为题目中所有的字符均为'0'或者'1',而且我们要匹配的字符串的长度最大为13(1-13),因此我们可以将这样一个长度的01串用一个十进制数来进行表示,比如0011串可以表示成3,串101可以表示为3.按照预测的规则,我们容易得知第N+1位置处的字符为满足最大的t且同时满足组大的k,使得s[k]s[k+1]...s[k+t-1]组成的字符串和s[N-t+1]s[N-t+2]...s[N]组成的字符串相等,且满足k<=N-t,则s[N+1]=s[k+t]。这样我们在求解时候可以保存前i个字符长度为j且字符串的状态为k(代表一个01串)时最靠右的位置,这样我们通过不断地更新这个dp数组,在i>=N时通过这个dp数组去不断地更新求解出我们最终的预测字符,说不清楚,反正代码写的很清楚。
 
  
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 1010010;
char s[maxn];
int dp[14][(1<<13)+10];
int N, L;

int main() {

    //freopen("aa.in", "r", stdin);

    while(scanf("%d %d", &N, &L) != EOF) {
        scanf("%s", s + 1);
        memset(dp, -1, sizeof(dp));
        for(int i = 1; i < N+L; ++i) {
            int tmp = 0;
            int pos = -1;
            for(int j = 0; j < 13 && j < i; ++j) {
                tmp |= ((s[i-j]-'0')<<j);
                if(dp[j+1][tmp] != -1) {
                    pos = dp[j+1][tmp];
                }
                dp[j+1][tmp] = i;
            }
            if(i >= N) {
                if(pos == -1) {
                    s[i+1] = '0';
                } else {
                    s[i+1] = s[pos+1];  //
                }
                //printf("Test: %d %d %c\n", i, pos, s[i+1]);
            }
        }
        s[N+L+1] = '\0';
        printf("%s\n", s+N+1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值