A. Train and Peter
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

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.
Sample test(s)
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.

字符串查找的水题,主要是练一下有关字符串的函数的用法。。

法一:strstr()与strrev()的用法

 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
int main()
{
char p[],s1[],s2[],*x,*xx;
while(~scanf("%s%s%s",p,s1,s2))
{
x = strstr(p,s1);//查找字符串s1在p中第一次出现的首位置,若查找不到返回NULL
if(x)
x = strstr(x+strlen(s1),s2);
strrev(p);//反转字符串p
xx = strstr(p,s1);
if (xx)
xx = strstr(xx+strlen(s1),s2);
if (xx&&x)
printf("both\n");
else if (x)
printf("forward\n");
else if (xx)
printf("backward\n");
else
printf("fantasy\n");
}
return ;
}

法二:string中find()与reverse()的用法

 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std; int main()
{
string p,s1,s2;
int pos = string::npos;//npos表示保证大于任何有效下标的值,即查找失败返回的值
while(cin>>p>>s1>>s2)
{
int flag1 = ,flag2 = ;
int x = p.find(s1);
if (x!=pos&&p.find(s2,x+s1.length())!=pos)
flag1 = ;
reverse(p.begin(),p.end());
x = p.find(s1);
if (x!=pos&&p.find(s2,x+s1.length())!=pos)
flag2 = ;
if (flag1&&flag2)
cout<<"both"<<endl;
else if (flag1)
cout<<"forward"<<endl;
else if (flag2)
cout<<"backward"<<endl;
else
cout<<"fantasy"<<endl;
}
return ;
}

A. Train and Peter的更多相关文章

  1. Codeforces Beta Round #8 A. Train and Peter KMP

    A. Train and Peter 题目连接: http://www.codeforces.com/contest/8/problem/A Description Peter likes to tr ...

  2. [数据结构]KMP小结

    KMP小结   By Wine93 2013.9 1.学习链接: http://www.matrix67.com/blog/archives/115 2.个人小结 1.KMP在字符串中匹配中起着巨大作 ...

  3. codeforces8A

    Train and Peter CodeForces - 8A Peter likes to travel by train. He likes it so much that on the trai ...

  4. 如何写一个拼写检查器-by Peter Norvig

    本文原著:Peter Norvig  中文翻译:徐宥 上个星期, 我的两个朋友 Dean 和 Bill 分别告诉我说他们对 Google 的快速高质量的拼写检查工具感到惊奇. 比如说在搜索的时候键入 ...

  5. Peter Hessler和他的中国三部曲(上)

    大约一年前,我从<英语铺子>栏目知道了Peter Hessler这位作家.主播分享了她的一些读后感和印象深刻的片段,当然主要是主播的声音太甜了,让我对这位美国作家留下了深刻的印象. Pet ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. 清华学堂 列车调度(Train)

    列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...

  8. Organize Your Train part II-POJ3007模拟

    Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Description RJ Freight, a Japane ...

  9. (转) How to Train a GAN? Tips and tricks to make GANs work

    How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...

随机推荐

  1. 14mysql事务、数据库连接池、Tomcat

    14mysql事务.数据库连接池.Tomcat-2018/07/26 1.mysql事务 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. MySQL手动控制事务 开启事 ...

  2. 每日命令:(1)ls

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  3. Swoft 新手向教程 - 通过 Docker 搭建一个开发环境

    本系列文章将从使用层面介绍 Swoft 框架的使用及业务开发,面向初中级的 PHPer Swoft首个基于 Swoole 原生协程的新时代 PHP 高性能协程全栈组件化框架,内置协程网络服务器及常用的 ...

  4. Xcode 出现Thread 1: signal SIGABRT

    代码语言:C 出现原因:数组初始化时,循环赋值越界. 例 bool type [30]; for (int i = 0;i<100;i++) type = 0;

  5. 利用 Python 批量修改文件名

    开发的第一步,首先得熟悉脚本中使用的模块函数,废话不多说,开干! 1 函数介绍 1.1 os 模块 (1)os.lisdir() >>> import os >>> ...

  6. 洛谷 1182 数列分段Section II

    [题解] 最大值最小化,那么一般要联想到二分.二分一个最大值,然后check一下能否分成小于等于m段即可. #include<cstdio> #include<algorithm&g ...

  7. java 中序列化(Serializable)

    18年5月份,在毕业快一年的时候,我终于把自己从研发助理偏向行政打杂,偶尔写一些前端代码的处境,回归到了初衷-安安静静写前后端代码的小小程序员~过程中的心酸也就不在这里吐啦,毕竟我是小太阳,永不熄灭的 ...

  8. RequestMapping_请求方式

    [映射请求参数.请求方法或请求头] 1.@RequestMapping 除了可以使用请求URL 映射请求外,还可以使用请求方法.请求参数及请求头映射请求. 2.@RequestMapping 的val ...

  9. DataFrame NaN 替换为零

    一个DataFrame 其中有空值NaN,将其替换为0: df.fillna(0) 如果将第一列替换为0: df[1].fillna(0,inplace=True)

  10. 【Codeforces 1129A】Toy Train

    [链接] 我是链接,点我呀:) [题意] 火车从1,2,3...n->1的方式绕圈走.(即每次从i走到i+1) 有一些点有货物需要装载,但是每个点只能装上去一个货物. 每个货物都有目标点卸货点( ...