Binary Witch
Time Limit: 1000MS   Memory Limit: 65536K
     

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 aN-t+1, aN-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 = aN-t+1, ak+1 = aN-t+2, ..., ak+t-1 = aN then the predicted value will be ak+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 aN+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
 
题意:
给一个长为n的字符串s,在前面找到一个长为m(m<=13)的字符串a=后缀
在m尽量大的前提下,使找到的字符串a尽量靠后
有则在字符串s末尾添加 字符串a的最后一个字符的后一个字符
没有则在字符串s末尾添加 0
连续找L次
最后输出字符串s的最后L位
 
逆序kmp
题目要求统计的是最长后缀,将原字符串翻转,就是最长前缀
然后对于接下来的每一天做一次kmp
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
char s[maxn],str[maxn];
int next[maxn];
char ans[];
int n,l;
int start=,len;
void getnext(char *st)
{
int j,lm=strlen(st);
for(int i=;i<lm;i++)
{
j=next[i];
while(j&&st[j]!=st[i]) j=next[j];
next[i+]= st[i]==st[j] ? j+ : ;
}
}
int kmp(char *p)
{
int j=,c=,idx;
int lm=strlen(p);
for(int i=start+;i<start+len;i++)
{
while(j&&p[j]!=str[i]) j=next[j];
if(p[j]==str[i]) j++;
if(j>c) { c=j; idx=i; }
if(j==lm) return i-j;
}
if(c) return idx-c;
return -;
}
int main()
{
char tmp[];
scanf("%d%d",&n,&l);
scanf("%s",s);
len=strlen(s);
for(int i=;s[i];i++) str[start+i]=s[len--i];
int cnt=l,k;
str[start+len]='\0';
while(cnt--)
{
strncpy(tmp,str+start,);
if(len<) tmp[len]='\0';
else tmp[]='\0';
getnext(tmp);
k=kmp(tmp);
start--;
len++;
if(k==-) str[start]='';
else str[start]=str[k];
}
for(int i=;i<l;i++) ans[i]=str[start+l--i];
ans[l]='\0';
printf("%s",ans);
}

poj 2541 Binary Witch的更多相关文章

  1. POJ 2541 Binary Witch(逆序KMP,好题)

    逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...

  2. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  3. poj 1430 Binary Stirling Numbers

    Binary Stirling Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1761   Accepted ...

  4. poj 1147 Binary codes

    Binary codes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5647   Accepted: 2201 Desc ...

  5. Poj 2499 Binary Tree(贪心)

    题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...

  6. POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)

    题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...

  7. POJ 2499 Binary Tree

    题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...

  8. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...

  9. POJ 2499 Binary Tree(二叉树,找规律)

    题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...

随机推荐

  1. Switches and Lamps(思维)

    You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This inform ...

  2. 常用web资源

    ip相关 新浪:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=220.181.38.110 (不带参数本机) ...

  3. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  4. 最多水容器(M)

    题目 给定n个非负整数a 1,a 2,...,a n,其中每个代表坐标(i,a i)处的一个点.绘制n条垂直线,使得线i的两个端点处于(i,a i)和(i,0)处.找到两条线,它们与x轴一起形成一个容 ...

  5. php 生成短网址 代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. DDL、DML和DCL的比较【引用学习】

    1.DDL       1-1.DDL的概述                DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以 ...

  7. [计算机网络] C++模拟telnet登陆SMTP服务发送邮件过程

    在百度文库中的<使用telnet协议收发邮件>,我们可以很清楚地看到如何通过telnet来进行发送邮件,下面是一些需要用到的命令,通过以下命令可以很容易实现邮件发送功能.为了更好地理解其中 ...

  8. [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

    通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...

  9. 协程-Greenlet

    协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈. 线程切换的时候会保存到CPU里面. 因此: 协程能保留上一次调用时的 ...

  10. BZOJ1040 骑士 【环套树 树形dp】

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 5611  Solved: 2166 [Submit][Stat ...