• codeforces8A


    Train and Peter

     CodeForces - 8A 

    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.

    Examples

    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.

    sol:直接字符串哈希后暴力匹配即可

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    typedef unsigned long long ull;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005,Power=97;
    int n,n1,n2;
    char S[N],S1[N],S2[N];
    ull Bin[N],Hash[N],H1=0,H2=0;
    inline void Pre(char *S)
    {
        int i;
        for(i=1;i<=n;i++)
        {
            Hash[i]=Hash[i-1]+Bin[i]*(S[i]-'a');
        }
    }
    inline ull Ask(int l,int r)
    {
        return (Hash[r]-Hash[l-1]);
    }
    inline bool Solve()
    {
        int i,Last=0;
        bool Flag=0;
        for(i=n1;i<=n;i++)
        {
            if(!Flag)
            {
                if(Ask(i-n1+1,i)==H1*Bin[i-n1]) Flag=1; Last=i;
            }
            else if(i-Last>=n2)
            {
                if(Ask(i-n2+1,i)==H2*Bin[i-n2]) return true;
            }
        }
        return false;
    }
    int main()
    {
        int i;
        bool ans1=0,ans2=0;
        scanf("%s%s%s",S+1,S1+1,S2+1);
        n=strlen(S+1); n1=strlen(S1+1); n2=strlen(S2+1);
        Bin[0]=1;
        for(i=1;i<=n;i++)
        {
            Bin[i]=Bin[i-1]*Power;
        }
        for(i=1;i<=n1;i++) H1=H1+Bin[i]*(S1[i]-'a');
        for(i=1;i<=n2;i++) H2=H2+Bin[i]*(S2[i]-'a');
        Pre(S);
        ans1=Solve();
        reverse(S+1,S+n+1);
        Pre(S);
        ans2=Solve();
        if(ans1)
        {
            if(!ans2) puts("forward");
            else puts("both");
        }
        else if(ans2)puts("backward");
        else puts("fantasy");
        return 0;
    }
    /*
    Input
    atob
    a
    b
    Output
    forward
    
    Input
    aaacaaa
    aca
    aa
    Output
    both
    */
    View Code
  • 相关阅读:
    2017-4-6校内训练
    [BZOJ]1023: [SHOI2008]cactus仙人掌图
    [BZOJ]1086: [SCOI2005]王室联邦
    [BZOJ]1055: [HAOI2008]玩具取名
    [BZOJ]3527: [Zjoi2014]力
    [BZOJ]2820: YY的GCD
    【BZOJ3233】【tyvj1729】文艺平衡树
    【数据结构】【平衡树】无旋转treap
    【BZOJ2733】【HNOI2012】永无乡
    【BZOJ3224】【tyvj1728】普通平衡树
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10753432.html
Copyright © 2020-2023  润新知