A. Train and Peter
1 second
64 megabytes
standard input
standard 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.
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.
字符串查找的水题,主要是练一下有关字符串的函数的用法。。
法一:strstr()与strrev()的用法
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
int main()
{
char p[],s1[],s2[],*x,*xx;
while(~scanf("%s%s%s",p,s1,s2))
{
x = strstr(p,s1);//查找字符串s1在p中第一次出现的首位置,若查找不到返回NULL
if(x)
x = strstr(x+strlen(s1),s2);
strrev(p);//反转字符串p
xx = strstr(p,s1);
if (xx)
xx = strstr(xx+strlen(s1),s2);
if (xx&&x)
printf("both\n");
else if (x)
printf("forward\n");
else if (xx)
printf("backward\n");
else
printf("fantasy\n");
}
return ;
}
法二:string中find()与reverse()的用法
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std; int main()
{
string p,s1,s2;
int pos = string::npos;//npos表示保证大于任何有效下标的值,即查找失败返回的值
while(cin>>p>>s1>>s2)
{
int flag1 = ,flag2 = ;
int x = p.find(s1);
if (x!=pos&&p.find(s2,x+s1.length())!=pos)
flag1 = ;
reverse(p.begin(),p.end());
x = p.find(s1);
if (x!=pos&&p.find(s2,x+s1.length())!=pos)
flag2 = ;
if (flag1&&flag2)
cout<<"both"<<endl;
else if (flag1)
cout<<"forward"<<endl;
else if (flag2)
cout<<"backward"<<endl;
else
cout<<"fantasy"<<endl;
}
return ;
}
A. Train and Peter的更多相关文章
- Codeforces Beta Round #8 A. Train and Peter KMP
A. Train and Peter 题目连接: http://www.codeforces.com/contest/8/problem/A Description Peter likes to tr ...
- [数据结构]KMP小结
KMP小结 By Wine93 2013.9 1.学习链接: http://www.matrix67.com/blog/archives/115 2.个人小结 1.KMP在字符串中匹配中起着巨大作 ...
- codeforces8A
Train and Peter CodeForces - 8A Peter likes to travel by train. He likes it so much that on the trai ...
- 如何写一个拼写检查器-by Peter Norvig
本文原著:Peter Norvig 中文翻译:徐宥 上个星期, 我的两个朋友 Dean 和 Bill 分别告诉我说他们对 Google 的快速高质量的拼写检查工具感到惊奇. 比如说在搜索的时候键入 ...
- Peter Hessler和他的中国三部曲(上)
大约一年前,我从<英语铺子>栏目知道了Peter Hessler这位作家.主播分享了她的一些读后感和印象深刻的片段,当然主要是主播的声音太甜了,让我对这位美国作家留下了深刻的印象. Pet ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- 清华学堂 列车调度(Train)
列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...
- Organize Your Train part II-POJ3007模拟
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Description RJ Freight, a Japane ...
- (转) How to Train a GAN? Tips and tricks to make GANs work
How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...
随机推荐
- TWaver 3D应用于大型数据中心(续)
在2014年11月份,我们当时发了一篇有关TWaver HTML5 3D应用于大型数据中心的文章,该blog比较详细的描述一些常用的功能的实现方法,比如:动态添加机柜,告警,温度,湿度等相关的功能的具 ...
- layui 动态表格之合并单元格
需求: 下面用excel表格大概模拟下需求,左边是原来的,要改成右边这样的: ①第一步:再生成表格后调用此方法,以合并重复的单元格 done : function(res, curr, count) ...
- Journals in Fluid Mechanics
journal of fluid mechanics physics of fluids annual review of fluid mechanics
- Quadtrees(四分树)
uva 297 Quadtrees Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Subm ...
- WebSocket客户端学习
1. WebSocket是一种网络通讯协议 参考文档:http://www.ruanyifeng.com/blog/2017/05/websocket.html https://github.com/ ...
- HDU 1253 三维数组的图上找最短路
题目大意: 从三维空间的(0,0,0)出发到(a-1,b-1,c-1),每移动一个都要时间加一,计算最短时间 根据六个方向,开个bfs,像spfa那样计算最短路径就行了,但是要1200多ms,也不知道 ...
- [luoguP1095] 守望者的逃离(DP)
传送门 这题....得考虑一些奇奇怪怪的复杂情况 不过也有简便方法. 枚举时间,先算出来只用魔法走的时间. 然后再枚举一遍时间,再算只走的时间,两个比较一下,取最游值. 代码 #include < ...
- noip模拟赛 铺瓷砖
[问题描述]有一面很长很长的墙. 你需要在这面墙上贴上两行瓷砖. 你的手头有两种不同尺寸的瓷砖, 你希望用这两种瓷砖各贴一行.瓷砖的长可以用分数表示,贴在第一行的每块瓷砖长度为A/B贴在第二行的每块瓷 ...
- JRobin cpu 和 磁盘
https://blog.csdn.net/li_zhongnan/article/details/3754053 https://blog.csdn.net/li_zhongnan/article/ ...
- 夜话JAVA设计模式之策略模式
策略模式 定义了算法簇,分别封装起来,让他们之间可以互相替换,让算法簇的变化独立于使用算法的客户.设计原则1 找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混在 ...