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 ...
随机推荐
- 题解 洛谷P3203/BZOJ2002【[HNOI2010]弹飞绵羊】
裸的LCT,关键是要怎么连边,怎么将这种弹飞关系转化成连边就行了. 那么我们可以这样连边: 一个节点i的爸爸就是i+ki. 没有i+ki那么就被弹飞了,即\(i\)的爸爸是虚拟节点n+1. 那么怎么求 ...
- UVA - 1620 Lazy Susan(逆序数)
题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...
- Address already in use: JVM_Bind:8080错误的解决办法
解决办法:先到命令行查找8080端口号被那个占用,输入后面的命令:netstat -ano 查到 然后到任务管理器把PID为20904的进程给结束掉,就OK了 -------------------- ...
- linux cut-连接文件并打印到标准输出设备上
博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 cut命令用来显示行中的指定部分,删除文件中指定字段.cut经常用来显示文件的内容,类似于下的type命令. 说明:该命令有两 ...
- Grid Convergence Index-- Post Processing in CFD
t Grid Convergence Index Table of Contents 1. Grid/mesh independence GCI 1.1. Richardson extrapola ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
- 数位dp备忘录
hdu 4734:把限制值化为数组形式,逐位求解 hdu 4507:类似于上题,其中求平方和的方法很妙 SPOJ BALNUM Balanced Numbers:经典数位dp,注意两点,1,不要把前 ...
- C语言基础--数据
c语言中数据: 在8位单片机种最常用的数据类型就是: unsigned char: 无符号字符型,位宽1个字节,8个位,表示的范围0~255(2^8-1) 在32位单片机中最常用的数据类型就是: un ...
- Android三角标签View:TriangleLabelView
Android三角标签View:TriangleLabelView 在一些商城.产品推销类APP中,如淘宝.京东.电影门票销售.商品降价促销这类的APP,常常会在其APP中看到,某些商品的左上角 ...
- MyBatis启动:MapperStatement创建
参考:http://blog.csdn.net/ashan_li/article/details/50351080 MappedStatement说明 一个MappedStatement对象对应Map ...