Codeforces Beta Round #8A Train and Peter (use of String)

Source: Internet
Author: User


Train and Petertime limit:1000msmemory limit:65536kbthis problem would be judged onCodeforces. Original id:8a
64-bit integer IO format:%i64dJava class Name:(Any)Prev Submit Status Statistics discuss Next

Peter likes to travel by train. He likes it so much the train he falls asleep.

Once in summer Peter is 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 have a flag of a particular Colo ur.

The boy started to memorize the order of the "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 he parents on what he is doing, and wrote the sequences of the colours that he had seen And after his sleep, respectively.

Peter ' s parents know that their son likes to fantasize. They give you the list of the "Colours" at the stations, the train passes sequentially on the the-the-the-A- D ask you to find out if Peter could see those sequences on the the the-the-to-B, or from-B to a. Remember, please, that Pe Ter had and 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 5, the string consists of Ercase Latin letters-the Flags ' colours at the stations on the the-the-A-B. On the the-the-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 is non-empty, consist of lowercase Latin letters, and the length of each does not exceed letters. Each of the sequences are written in chronological order.

Output

Output one of the four words without inverted commas:

    • ? forward? -if Peter could see such sequences-on the the-the-same-from-B;
    • ? backward? -if Peter could see such sequences on the the the-the-from B
    • ? both? -if Peter could see such sequences both on the the the-the-to-B, and on the-the-the-the-the-the-from B
    • ? Fantasy? -if Peter could not see such sequences.

Sample Inputinput
Atob
A
B
Output
Forward
Input
Aaacaaa
Aca
Aa
Output
Both
Hint

It is assumed that the train moves all the time and so one flag cannot be seen twice. There is no flags at stations A and B.


Test Instructions: Enter three strings, S,a,b,s is the source string, and a, B is the target string, ask if you can get a string in sequence , or vice versa.


This problem can be simulated with strings, but C + + has a powerful string class, how can not use it?

Read the code of the Q God, "admire" two words spontaneously!


by the way, fill in the relevant functions:

Lookup (Find)
Grammar:
Size_type Find (const basic_string &STR, size_type index);
Size_type Find (const char *STR, size_type index);
Size_type Find (const char *STR, size_type index, size_type length);
Size_type Find (char ch, size_type index);

The Find () function:

Returns the position of the first occurrence of STR in the string, starting with index. If not found, return to String::npos,
Returns the position of the first occurrence of STR in the string, starting with index and length. If you don't find it, return to String::npos.
Returns the position of the first occurrence of the character ch in the string (found from index). If you don't find it, return to String::npos.
For example:

String str1 ("Alpha Beta Gamma Delta"); unsigned int loc = Str1.find ("Omega", 0); if (loc! = string::npos)    cout < < "Found Omega at" << Loc << endl;else    cout << "didn ' t find Omega" << endl;//print this

AC Code:

#include <iostream> #include <string> #include <algorithm>using namespace Std;int main () {    string S,a,b;    while (cin>>s>>a>>b)    {        bool isok[2]= {0,0};        for (int i=0; i<2; i++)        {            int idx=s.find (a);            if (Idx!=string::npos)            {                idx=s.find (b,idx+a.size ());                if (idx!=string::npos)                    isok[i]=1;            }            Reverse (S.begin (), S.end ());        }        if (isok[0]&&isok[1])            cout<< "both" <<endl;        else if (isok[0])            cout<< "forward" <<endl;        else if (isok[1])            cout<< "Backward" <<endl;        else            cout<< "Fantasy" <<endl;    }    return 0;}



Codeforces Beta Round #8A Train and Peter (use of String)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.