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. 【转】cpu的核心数与线程数的关系

    原文地址:http://www.dn580.com/dnzs/dncs/2013/10/08/172948914.html 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能 ...

  2. es6从零学习(一)let 和 const 命令

    es6从零学习(一):let 和 const 命令 一:let 变量 1.块级作用域{}:let只在自己的块级作用域内有效. for(let i =0;i<3;i++) { console.lo ...

  3. c++ string需要注意的地方

    There are multiple answers based on what you are doing with the string. 1) Using the string as an id ...

  4. Swift 泛型和闭包结合使用

    通常在Swift中定义一个闭包来使用 typealias Closure= (Any?) -> () var tempClosure :Closure? /// 定义一个方法直接调用 func ...

  5. 【.NET】- async await 异步编程

    为什么需要异步,异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源的访问有时很慢或会延迟. 如果此类活动在同步过程中受阻,则整个应用程序必须等待. 在异步过程中, ...

  6. Python文件操作:同一个文件进行内容替换

    在原文件上进行部分内容的替换,主要用到seek()函数和truncate()函数实现,直接上代码: ) # 将指针位置指到文件开头(注意:一定要有这步操作,不然无法清空文件)f1.truncate() ...

  7. google go语言开发

    C:cd C:\Program Files\go\gopath\src\opmsset GOOS=linuxset GOARCH=amd64set CGO_ENABLED=0make.batgo in ...

  8. PL/SQL在 win8.1系统下连接Oracle11g没有database处理方法(亲身实验,吐血分享)

    一.问题 这里首先说明下我的环境:win8.1(64bit)+oracle11g(64bit)+PL/SQL(32bit).状况是:net manager正常配置,测试也成功,但是用PL/SQL连接的 ...

  9. 第33天:封装自己的class类

    封装自己的class类,实现浏览器兼容. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  10. windows 2008 iis7 上传大文件限制的真正解决办法

    以前做了一个网站 ,当时本机测试时上传文件大小没有问题,上G也应该可以,可是放在服务器后只能上传小于30M以下文件,当时基本需要也基本在30M以下,就没有管,后在网上发现原来是window2008本身 ...