博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2541 Binary Witch
阅读量:6070 次
发布时间:2019-06-20

本文共 2817 字,大约阅读时间需要 9 分钟。

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 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 71101110010

 

Sample Output

0100100

 

Source

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

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/6955699.html

你可能感兴趣的文章
POJ 3261 后缀数组
查看>>
SQL 语言分类
查看>>
CTSC2018 青蕈领主
查看>>
Linux Shell 程序调试
查看>>
[javaSE] 看知乎学习反射
查看>>
Python读取redis数据
查看>>
删除treeview下的节点(包括子节点),不管在第几层
查看>>
SMARTFORM 小技巧
查看>>
一道练习题引发的思考
查看>>
Nancy 返回值详解
查看>>
ASP.NET MVC Form验证
查看>>
通过源码了解ASP.NET MVC 几种Filter的执行过程
查看>>
配置文件——节点<machineKey>的作用,强随机生成
查看>>
net.sf.json.JSONObject的json字符串转对象
查看>>
回溯法
查看>>
大作业:电梯设计的概要设计文档
查看>>
扑克游戏
查看>>
Android之LayoutInflater详解
查看>>
BZOJ-3172: [Tjoi2013]单词 (AC自动姬 fail树)
查看>>
Java 集合深入理解(7):ArrayList
查看>>