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);
}

最新文章

  1. 51nod 1422(强行YY)
  2. ThinkPHP的RBAC
  3. Product of Array Exclude Itself
  4. RabbitMQ系列二(构建消息队列)
  5. 如何安装php?
  6. GDI+中GIF图片的显示
  7. Js 获取当前时间
  8. svn:revert to this version 和 revert changes from this version的区别 假设我们有许多个版本,版本号分别是1-10
  9. Csharp 高级编程 C7.1.2(2)
  10. react native调试
  11. hibernate框架基础描述
  12. spring boot 2.0 集成 shiro 和 pac4j cas单点登录
  13. [译]在Linux上的提高MySQL/MariaDB安全性的12条建议
  14. C#容器类,性能介绍
  15. 关于linux系统如何实现fork的研究(一)
  16. python2和python3的主要区别
  17. MySQL5.6复制技术(2)-主从部署以及半同步配置详细过程
  18. 安装Windows Server 2012 R2提示&quot;unable to create a new system partition or locate an existing system partition&quot;解决方法
  19. 02: Redis缓存系统
  20. 讲解java异常

热门文章

  1. Mybatis中resultMap与resultType区别
  2. 20145214实验五 Java网络编程及安全
  3. [zt]手把手教你写对拍程序(PASCAL)
  4. xml解析----java中4中xml解析方法(转载)
  5. lintcode-179-更新二进制位
  6. Swift-map()跟flatMap()区别
  7. &lt;T extends Comparable&lt;? super T&gt;&gt;
  8. 【Docker 命令】- inspect命令
  9. react项目开发入门
  10. 《Effective C#》快速笔记(三)- 使用 C# 表达设计