字符串——Train and Peter

Train and Peter

题面翻译

【题目描述】

Peter 很喜欢坐火车去旅行。他实在是太喜欢坐火车了,以至于在火车上睡着了(雾)。

在一个夏天,Peter 正在一个从 A 城开往 B 城的列车上。像往常一样,他睡着了。突然,他醒了过来(?),开始看窗外的景物。他发现每一个火车站都有一面彩旗(旗上只有一种颜色)。

Peter 开始记忆他看到的彩旗的顺序。但是很快地,他又睡着了。不幸地,他没睡多久又醒来了,并且他开始继续记他看到的彩旗。一段时间以后,他又睡着了,这次直到旅途结束都没醒来。

到站以后,他告诉了他的父母他在旅途中看到的彩旗的顺序——在他中途睡着之前和中途睡着之后分别看到的两个颜色序列。

他的父母知道 Peter 特别喜欢幻想,所以他们把 Peter 说的序列给了你,并请你判断他的乘车方向。

他的父母用不同的小写字母表示不同的颜色。相同字母表示相同颜色,不同字母表示不同颜色。

【输入格式】

输入共有三行。第一行包括一个连续的,不为空且只有小写字母的字符串 s 0 s_0 s0,表示从 A 到 B 依次每个站的彩旗的颜色(A 站和 B 站没有彩旗)。列车从 B 到 A 也会经过同样的车站——以相反的顺序而已。

第二行和第三行分别为 Peter 两次看到的序列 s 1 s_1 s1 s 2 s_2 s2。两个序列都包括连续的小写字母且不为空。

【输出格式】

一行,输出以下四种单词之一(不包括引号):

forward:Peter 只可能在从 A 到 B 的旅途中看到这样的序列。

backward:Peter 只可能在从 B 到 A 的旅途中看到这样的序列。

both:Peter 既可能在从 A 到 B 的旅途中看到,也可能在从 B 到 A 的旅途中看到这样的序列。

fantasy:Peter 不可能看到这样的序列。

【补充说明】

列车一直都在移动,所以一面同样的旗子不可能被看到两次。

题目描述

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 $ 10^{5} $ , 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.

样例 #1

样例输入 #1

atob
a
b

样例输出 #1

forward

样例 #2

样例输入 #2

aaacaaa
aca
aa

样例输出 #2

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.

思路

本道题其实就是判断第二行第三行在第一行字符串的相对位置(对于正向的位置我们就正向用find,对于反向的我们可以把字符串翻转过来)。

  • 细节1:字符串的find使用细节:当find找不到值就会返回-1。
  • 细节2:find里面可以放第二个参数(意思就是从xx位置之后看)。

因此,我们可以认为所有字符串都可以满足forward的情况,因此当出现不满足forward的情况就是backward

代码

#include<iostream>
#include<algorithm>

using namespace std;

string a,b,c;
int n;

int main(){
    cin>>a>>b>>c;
    
    int op1=0,op2=0;
    
    n=a.size();
    
    if(a.find(b)<=n&&a.find(c,a.find(b)+b.size())<=n){//第一个判断
		op1=1;
	}
	reverse(a.begin(),a.end());
    
	if(a.find(b)<=n&&a.find(c,a.find(b)+b.size())<=n){
		op2=1;
	}
    
    
    if(op1==1&&op2==1){
        puts("both");
    }else if(op1==1){
        puts("forward");
    }else if(op2==1){
        puts("backward");
    }else{
        puts("fantasy");
    }
    
    return 0;
}
  • 16
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值