A. Train and Peter

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()的用法

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string>
 5 #include <string.h>
 6 using namespace std;
 7 int main()
 8 {
 9     char p[100002],s1[102],s2[102],*x,*xx;
10     while(~scanf("%s%s%s",p,s1,s2))
11     {
12         x = strstr(p,s1);//查找字符串s1在p中第一次出现的首位置,若查找不到返回NULL
13         if(x)
14             x = strstr(x+strlen(s1),s2);
15         strrev(p);//反转字符串p
16         xx = strstr(p,s1);
17         if (xx)
18             xx = strstr(xx+strlen(s1),s2);
19         if (xx&&x)
20             printf("both
");
21         else if (x)
22             printf("forward
");
23         else if (xx)
24             printf("backward
");
25         else
26             printf("fantasy
");
27     }
28     return 0;
29 }
View Code

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

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string>
 5 #include <string.h>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     string p,s1,s2;
11     int pos = string::npos;//npos表示保证大于任何有效下标的值,即查找失败返回的值
12     while(cin>>p>>s1>>s2)
13     {
14         int flag1 = 0,flag2 = 0;
15         int x = p.find(s1);
16         if (x!=pos&&p.find(s2,x+s1.length())!=pos)
17             flag1 = 1;
18         reverse(p.begin(),p.end());
19         x = p.find(s1);
20         if (x!=pos&&p.find(s2,x+s1.length())!=pos)
21             flag2 = 1;
22         if (flag1&&flag2)
23             cout<<"both"<<endl;
24         else if (flag1)
25             cout<<"forward"<<endl;
26         else if (flag2)
27             cout<<"backward"<<endl;
28         else
29             cout<<"fantasy"<<endl;
30     }
31     return 0;
32 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3652758.html