Codeforces Beta Round #8 A. Train and Peter(kmp)

A. Train and Peter
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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.
Examples
inputCopy
atob
a
b
outputCopy
forward
inputCopy
aaacaaa
aca
aa
outputCopy
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.

题意

给你一个字符串,和两个子串,先正向扫一遍主串,判断两个子串是不是在主串中,并且不想交,再反向扫一遍,看是否在主串中。

思路

正向一遍KMP,反向一遍KMP,判断两个子串时候能够存在就行了。
注意:查完第一个字符串之后,要从剩下的主串中查找第二个字符串,因为第二个字符串有可能会在第一个字符串的前面。

代码

#define push_back pb
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1);
const int maxn=100000+10;
const int maxm=100000+10;
const int INF = 0x3f3f3f3f;
using namespace std;
int next[maxn];
void getnext(string t)
{
    next[0]=-1;
    int j=0,k=-1;
    int len=t.length();
    while(j<len)
    {
        if(k==-1||t[j]==t[k])next[++j]=++k;
        else k=next[k];
    }
}

int kmp(string s,string t)
{
    int i=0,j=0;
    int lens=s.length(),lent=t.length();
    while(i<lens&&j<lent)
    {
        if(j==-1||s[i]==t[j]) i++,j++;
        else j=next[j];
        if(j==lent) return i-j+1;
    }
    return -1;
}
int main()
{
    string s,t1,t2;
    cin>>s>>t1>>t2;
    getnext(t1);
    int lens=s.length(),len1=t1.length(),len2=t2.length();

    int p11=kmp(s,t1);
    string st(s,p11-1+len1,s.length()-1);
    reverse(s.begin(),s.end());

    int p21=kmp(s,t1);
    string st2(s,p21-1+len1,s.length()-1);
    getnext(t2);

    int p12=kmp(st,t2);
    int p22=kmp(st2,t2);

    int f1=0,f2=0;
    if(p11!=-1&&p12!=-1) f1=1;
    if(p21!=-1&&p22!=-1) f2=1;
    if(f1&&f2) cout<<"both"<<endl;
    else if(f1) cout<<"forward"<<endl;
    else if(f2) cout<<"backward"<<endl;
    else cout<<"fantasy"<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值