Binary Witch

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.

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

Source

Northeastern Europe 2000, Far-Eastern Subregion

source code

#include<iostream>  
#include<cstring>  
using namespace std;  
   
bool T[1000010];  
bool P[14];  
int n,m,N,L;  
int pre[14];  
  
void COMPUTE_PREFIX_FUNCTION(bool *P,int m)  
{  
    pre[m] = 0;  
    int q = 0;  
    for (int i=m-1; i>=1; i--)  
    {  
        while(q>0 && P[m-q] != P[i])  
            q = pre[m-q+1];  
        if(P[m-q] == P[i])  
            q++;  
            pre[i] = q;  
    }  
}  
   
int RE_KMP_MATCHER(bool *T,bool *P,int n,int m)  
{  
    int mpos = 0;  
    COMPUTE_PREFIX_FUNCTION(P,m);  
    int q = 0;  
    for(int i=n; i>=1; i--)  
    {  
        while(q>0 && P[m-q] != T[i])  
            q = pre[m-q+1];  
        if(P[m-q] == T[i])  
            q++;  
        if(q==m)  
        {  
            mpos = i;  
            q = pre[m-q+1];  
            return mpos;  
        }  
    }  
    return 0;  
}  
  
int main()  
{  
    while(cin>>N>>L)  
    {  
        memset(T,0,sizeof(T));  
        char ch;  
        getchar();  
        for(int i=1; i<=N; i++)  
        {  
            scanf("%c",&ch);  
            T[i] = (ch=='1') ? 1 : 0;  
        }  
        int M;  
        int pos=0;  
        for(n=N; n<=N+L-1; n++) //当前文本的长度  //O(L) * O(13) * O(N)  
        {  
            if(n/2 < 13 )  
                M = n/2;  
            else  
                M = 13;  
            for(m=M; m>=1; m--) //枚举模式长度m  O(13) * O(N)  
            {  
                for(int i=1; i<=m; i++)  
                    P[i] = T[n-m+i];  
                pos = RE_KMP_MATCHER(T,P,n-1,m);   //O(N)  //这里,要逆序KMP  
                if(pos)  
                {  
                    T[n+1] = T[pos+m];  
                    break;  
                }  
            }  
        }  
        for(int i=1; i<=L; i++)  
            cout<<T[N+i];  
        cout<<endl;  
    }  
    return 0;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值