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. 机器学习之决策树二-C4.5原理与代码实现

    决策树之系列二—C4.5原理与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9435712.html I ...

  2. SmartCode 使用常见问题

    SmartCode 常见问题 SmartCode 能干什么? SmartCode = IDataSource -> IBuildTask -> IOutput => Build Ev ...

  3. Do you have an English name? 你有英文名吗?

    文中提到的所有人名都是虚构的,如有雷同,纯属巧合. 当然,你的洋名儿也可能是德文.法文.意大利文,等々々々. 全球化时代,和老外的交流也多了."高端"的程序员想要进欧美系外企,想要 ...

  4. 通过 JSONP 实现跨域请求

    优质参考资料:https://www.cnblogs.com/chiangchou/p/jsonp.html https://blog.csdn.net/hansexploration/article ...

  5. 结合JDK源码看设计模式——策略模式

    前言: 现在电商已经成为我们生活中不可或缺的购物渠道,同时各大商家会针对不同的时间做出不同的折扣,这在我们看来就是一种营销手段,也是一种策略,今天我们就来讲讲JDK中的策略模式是怎么样的. 一.定义 ...

  6. 可编辑且宽度自适应input

    默认的input项是比较难看的,并且它的宽度还无法随着输入而变化,这样未免有些呆板,不过借助JavaScript可以达到宽度自适应的效果,下面为了方便使用了jQuery: <div class= ...

  7. Fragment生命周期以及使用时的小问题

    前言- 昨天在写UI的时候用到了FRAGMENT,发现自己对此还不是非常了解,借此机会记录一下 Fragment的生命周期- 官方生命周期图: Fragment每个生命周期方法的意义.作用- onVi ...

  8. 微信小程序中通过腾讯地图进行逆地址解析报错message: "请求来源未被授权, 此次请求来源域名:servicewechat.com"

    在小程序中获取定位具体信息时,不要配置腾讯地图中的WebServiceAPI中的域名白名单什么的,域名配置直接在小程序后台中配置(就是这个货https://apis.map.qq.com), 千万千万 ...

  9. Hive参数

    1.hive当中的参数.变量都是以命名空间开头 2.通过${}方式进行引用,其中system.env下的变量必须以前缀开头 3.hive参数设置方式 1.修改配置文件${HIVE_HOME}/conf ...

  10. 从0开始的Python学习018更多的Python内容

    特殊的方法 之前学习的都是一些常用的方法,为了使我们的学习更加的完整,我们在这里学习一些特殊的方法. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[key]这样的索引操作( ...