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. java string与byte互转

    1.string 转 byte[]byte[] midbytes=isoString.getBytes("UTF8");//为UTF8编码byte[] isoret = srt2. ...

  2. Socket中BufferedReader.readLine()的阻塞特性导致的数据无法多次发送的问题

    https://blog.csdn.net/shenpibaipao/article/details/70236657

  3. C语言之自定义__DATE__与__TIME__

    /******************************************************************* * > File Name: 05-ymd.c * &g ...

  4. BZOJ 3993 Luogu P3324 [SDOI2015]星际战争 (最大流、二分答案)

    字符串终于告一段落了! 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3993 (luogu) https://www.l ...

  5. ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 Scores

    #1236 : Scores 时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 Kyle is a student of Programming Monkey Element ...

  6. [luoguP1403] [AHOI2005]约数研究(这。。。)

    传送门 用类似筛法的原理,就好啦 ——代码 #include <cstdio> int n, ans; int a[1000001]; int main() { int i, j; sca ...

  7. vue组件知识总结

    vue组件化开发 将一个页面分割成若干个组件,一个页面js+css+html,将自己类容分割出来,方便开发,更好了维护我们的代码,每个组件封装自己的js+html+css,样式命名冲突 组件分类 页面 ...

  8. Linux RAR 解压缩

    1.下载 http://www.rarlab.com/download.htm 2.安装 tar zxvf rarlinux-3.8.0.tar.gz cd rar make make install ...

  9. NOIP2013 提高组合集

    NOIP 2013 提高组 合集 D1 T1 转圈游戏 快速幂裸题 #include <iostream> #include <cstdio> #include <cst ...

  10. 洛谷—— P1725 琪露诺

    https://www.luogu.org/problem/show?pid=1725 题目描述 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精.某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这 ...