Algorithm
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.
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 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.
atob
a
b
forward
aaacaaa
aca
aa
both
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.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <iostream>
int main(){
std::string trip, first, second;
getline(std::cin, trip);
getline(std::cin, first);
getline(std::cin, second);
long posF1 = trip.find(first);
long posF2 = trip.find(second);
while(posF2 != std::string::npos){
long test = trip.find(second, posF2 + 1);
if(test != std::string::npos){posF2 = test;}
else{break;}
};
bool forward = (posF1 != std::string::npos) && (posF2 != std::string::npos) && (posF1 + first.size() - 1 < posF2);
std::string retTrip = trip;
for(long p = 0; p < trip.size(); p++){retTrip[p] = trip[trip.size() - 1 - p];}
long posB1 = retTrip.find(first);
long posB2 = retTrip.find(second);
while(posB2 != std::string::npos){
long test = retTrip.find(second, posB2 + 1);
if(test != std::string::npos){posB2 = test;}
else{break;}
};
bool backward = (posB1 != std::string::npos) && (posB2 != std::string::npos) && (posB1 + first.size() - 1 < posB2);
if(forward && backward){puts("both");}
else if(forward && !backward){puts("forward");}
else if(!forward && backward){puts("backward");}
else {puts("fantasy");}
return 0;
}
Copy The Code &
Try With Live Editor
Input
a
b
Output
Demonstration
Codeforces Solution-A. Train and Peter-Solution in C, C++, Java, Python