codeforces8A
Train and Peter
Peter likes to travel by train. He likes it so much that on the train he falls asleep.
Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.
The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.
At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.
Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.
Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.
Input
The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order.
The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order.
Output
Output one of the four words without inverted commas:
- «forward» — if Peter could see such sequences only on the way from A to B;
- «backward» — if Peter could see such sequences on the way from B to A;
- «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A;
- «fantasy» — if Peter could not see such sequences.
Examples
atob
a
b
forward
aaacaaa
aca
aa
both
Note
It is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.
sol:直接字符串哈希后暴力匹配即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef unsigned long long ull;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,Power=;
int n,n1,n2;
char S[N],S1[N],S2[N];
ull Bin[N],Hash[N],H1=,H2=;
inline void Pre(char *S)
{
int i;
for(i=;i<=n;i++)
{
Hash[i]=Hash[i-]+Bin[i]*(S[i]-'a');
}
}
inline ull Ask(int l,int r)
{
return (Hash[r]-Hash[l-]);
}
inline bool Solve()
{
int i,Last=;
bool Flag=;
for(i=n1;i<=n;i++)
{
if(!Flag)
{
if(Ask(i-n1+,i)==H1*Bin[i-n1]) Flag=; Last=i;
}
else if(i-Last>=n2)
{
if(Ask(i-n2+,i)==H2*Bin[i-n2]) return true;
}
}
return false;
}
int main()
{
int i;
bool ans1=,ans2=;
scanf("%s%s%s",S+,S1+,S2+);
n=strlen(S+); n1=strlen(S1+); n2=strlen(S2+);
Bin[]=;
for(i=;i<=n;i++)
{
Bin[i]=Bin[i-]*Power;
}
for(i=;i<=n1;i++) H1=H1+Bin[i]*(S1[i]-'a');
for(i=;i<=n2;i++) H2=H2+Bin[i]*(S2[i]-'a');
Pre(S);
ans1=Solve();
reverse(S+,S+n+);
Pre(S);
ans2=Solve();
if(ans1)
{
if(!ans2) puts("forward");
else puts("both");
}
else if(ans2)puts("backward");
else puts("fantasy");
return ;
}
/*
Input
atob
a
b
Output
forward Input
aaacaaa
aca
aa
Output
both
*/
codeforces8A的更多相关文章
随机推荐
- Chrome - JavaScript调试技巧总结(浏览器调试JS)
Chrome 是 Google 出品的一款非常优秀的浏览器,其内置了开发者工具(Windows 系统中按下 F12 即可开启),可以让我们方便地对 JavaScript 代码进行调试. 为方便大家学习 ...
- 微信公众号开发C#系列-6、消息管理-普通消息接受处理
1.概述 通过前面章节的学习,我们已经对微信的开发有了基本的掌握与熟悉,基本可以上手做复杂的应用了.本篇我们将详细讲解微信消息管理中普通消息的接收与处理.当普通微信用户向公众账号发消息时,微信服务器将 ...
- group by搭配 order by解决排序问题
问题 Ftravel_id Facct_no Froute_code Fmodify_time 41010020180725102219102000010452 1359c027b0a15266418 ...
- React-代码复用(mixin.hoc.render props)
前言 最近在学习React的封装,虽然日常的开发中也有用到HOC或者Render Props,但从继承到组合,静态构建到动态渲染,都是似懂非懂,索性花时间系统性的整理,如有错误,请轻喷~~ 例子 以下 ...
- 剑指 offer 第一题: 二维数组中的查找
打算写 图解剑指 offer 66 题 的系列文章,不知道大家有没有兴趣
- 基于python的种子搜索网站-开发过程
本讲会对种子搜索网站的开发过程进行详细的讲解. 源码地址:https://github.com/geeeeeeeek/bt 项目开发过程 项目简介 该项目是基于python的web类库django开发 ...
- 学习安卓开发[5] - HTTP、后台任务以及与UI线程的交互
在上一篇学习安卓开发[4] - 使用隐式Intent启动短信.联系人.相机应用中了解了在调用其它应用的功能时隐式Intent的使用,本次基于一个图片浏览APP的开发,记录使用AsyncTask在后台执 ...
- 原 js实现数据持久化
在写js事件时,常常遇到点击一个事件,然后在若干时间以后需要知道最近一次点击的事件的结点.比如这里: 我点击树节点1,再点击tab2,然后我再来回切换tab,假如最后一次点击tab时在tab2上,这时 ...
- 解决laravel Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found 错误
这个错误的原因来自于没有安装一个依赖库: 官方文档说明如下: Modifying Columns Prerequisites Before modifying a column, be sure to ...
- <%@ Register TagPrefix="uc1" TagName="user" Src="../Control/user.ascx" %>什么意思?
TagPrefix定义控件位置的命名空间.有了命名空间制约,就可以在同一个网页里使用不同功能的同名控件. TagName指向所使用控件的名字.在同一个命名空间里的控件名是唯一的.控件名一般都表明控件的 ...