Train and Peter

CodeForces - 8A

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

Input
atob
a
b
Output
forward
Input
aaacaaa
aca
aa
Output
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的更多相关文章

随机推荐

  1. RAC(ReactiveCocoa)概括

    ReactiveCocoa(简称RAC,以下都用RAC)是github团队开源的一套基于Cocoa并且具有FRP(Functional Reactive Programming-响应式编程)特性的框架 ...

  2. WPF ResourceDictionary 主题资源替换(一)

    当我们需要在程序中替换主题,更换另一套背景.颜色.样式时,如何在不修改资源Key值,直接替换呢? 问题&疑问 1. Key值冲突 同一ResourceDictionary中,不可以使用相同Ke ...

  3. 文件的基本管理和XFS文件系统备份恢复

    4.1 Linux系统目录结构和相对/绝对路径 4.1.1系统目录结构 在WIN系统中,查看文件先进入相应的盘符,然后进入文件目录 在WIN中,它是多根  c:\    d:\   e:\ Linux ...

  4. Spring Boot 2.X 如何优雅的解决跨域问题?

    一.什么是源和跨域 源(origin)就是协议.域名和端口号.URL由协议.域名.端口和路径组成,如果两个URL的协议.域名和端口全部相同,则表示他们同源.否则,只要协议.域名.端口有任何一个不同,就 ...

  5. Java学习点滴——泛型

    基于<Java编程思想>第四版 前言 虽然Java的泛型在语法上和C++相比是类似的,但在实现上两者是全然不同的. 语法 Java只需要一个<>就可定义泛型.在<> ...

  6. jsp内置对象-pageContext对象

    1.概念:pageContext对象能够获取JSP页面中的request.response.session.application等其他内置对象.pageContext对象的创建和初始化由容器完成,可 ...

  7. CSS实现无外边框列表效果

    方法一:使用外层容器切割 给每一个 li 设定右边框和下边框线 把ul放置在一个外层div中,设定div的宽高,通过overflow:hidden将一部分li的边框隐藏 此方法只需要计算父容器的宽高, ...

  8. Android Studio教程07-Fragment的使用

    目录 1. Fragment是什么 1.1. 设计原理和实例 2. 创建fragment 2.1. fragment的生命周期 2.2 添加用户界面:融入到Activity中 3. 管理fragmen ...

  9. 一个特殊的SQL Server阻塞案例分析

    上周,在SQL Server数据库下面遇到了一个有意思的SQL阻塞(SQL Blocking)案例.其实个人对SQL Server的阻塞还是颇有研究的.写过好几篇相关文章. 至于这里为什么要总结一下这 ...

  10. 下载华为交换机MIB参考文件并使用snmpwalk获取OID信息

    1.下载交换机MIB参考文件访问 https://support.huawei.com/enterprise/zh/index.html,搜索交换机型号 点击案例库,选择MIB参考 下载文件 打开文件 ...