poj 2541 Binary Witch
Time Limit: 1000MS | Memory Limit: 65536K | |
Description
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
Output
Sample Input
10 7
1101110010
Sample Output
0100100
Source
#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的更多相关文章
- POJ 2541 Binary Witch(逆序KMP,好题)
逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- poj 1430 Binary Stirling Numbers
Binary Stirling Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1761 Accepted ...
- poj 1147 Binary codes
Binary codes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5647 Accepted: 2201 Desc ...
- Poj 2499 Binary Tree(贪心)
题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...
- POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)
题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...
- POJ 2499 Binary Tree
题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- POJ 2499 Binary Tree(二叉树,找规律)
题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...
随机推荐
- sqoop-1.4.6安装与使用
一.安装 1.下载sqoop-1.4.6-bin.tar.gz并解压 2.修改conf/sqoop-env.sh,设置如下变量: export HADOOP_COMMON_HOME=/usr/loca ...
- hadoop问题集(1)
参考: http://dataunion.org/22887.html 1.mapreduce_shuffle does not exist 执行任何时报错: Container launch ...
- 2019寒假训练营第三次作业part2 - 实验题
热身题 服务器正在运转着,也不知道这个技术可不可用,万一服务器被弄崩了,那损失可不小. 所以, 决定在虚拟机上试验一下,不小心弄坏了也没关系.需要在的电脑上装上虚拟机和linux系统 安装虚拟机(可参 ...
- iOS- 如何将非ARC的项目转换成ARC项目(实战)
1.前言 因为公司有个国外餐饮系统,编程开发了3-4年,之前用的都是非ARC,开发到今年,第一批迭代开发的人员早已不见,目前发现了有许多的内存泄露之类的,系统没有自动释放该释放的内存.一旦app长 ...
- Qt Meta Object system 学习
原文地址:http://blog.csdn.net/ilvu999/article/details/8049908 使用 meta object system 继承自 QOject 类定义中添加 Q_ ...
- 线程同步(使用了synchronized)和线程通讯(使用了wait,notify)
线程同步 什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题. 实现同步机制有两个方法:1.同 ...
- array to object
array to object native js & ES6 https://stackoverflow.com/questions/4215737/convert-array-to-obj ...
- 深入学习 Redis系列
深入学习 Redis(1):Redis 内存模型 深入学习 Redis(2):持久化 深入学习 Redis(3):主从复制 深入学习 Redis(4):哨兵
- RESTful Webservice
1,REST和RESTFUL是什么? REST ( REpresentational State Transfer ),State Transfer 为 "状态传输" 或 &quo ...
- Oracle DB_LINK如何使用
语句,或可通过可视化操作 -- Create database link create database link DBL_TESTconnect to UID identified by PSWus ...