CodeForces 8A Train and Peter (简单模拟题)
来源:互联网 发布:2017学生空间七天网络 编辑:程序博客网 时间:2024/05/16 00:58
1、http://codeforces.com/problemset/problem/8/A
2、题目大意:
给定一个字符串作为主串,后边有两个字符串连续的,判断后边的字符串正序和逆序是否都能找到
简单的字符串题目
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 exceed105, 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.
atobab
forward
aaacaaaacaaa
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.
#include<stdio.h>#include<string.h>#include<algorithm>char s[100005],sa[105],sb[105];int main(){ while(scanf("%s%s%s",s,sa,sb)!=EOF) { int l=strlen(s); int la=strlen(sa); int lb=strlen(sb); int flag1=0,pos=0,flag2=0,flag3=0,flag4=0; for(int i=0;i<l;i++) { flag1=1; for(int j=0;j<la;j++) { if(s[i+j]!=sa[j]) { flag1=0; break; } } if(flag1==1) { pos=i+la; break; } } if(flag1==1) { for(int i=pos;i<l;i++) { flag2=1; for(int j=0;j<lb;j++) { if(s[i+j]!=sb[j]) { flag2=0; break; } } if(flag2==1) break; } } //反着找 for(int i=l-1;i>=0;i--) { flag3=1; for(int j=0;j<la;j++) { if(s[i-j]!=sa[j]) { flag3=0; break; } } if(flag3==1) { pos=i-la; break; } } for(int i=pos;i>=0;i--) { flag4=1; for(int j=0;j<lb;j++) { if(s[i-j]!=sb[j]) { flag4=0; break; } } if(flag4==1) break; } if(flag2==1 && flag4==1) { printf("both\n"); } else if(flag2==1) { printf("forward\n"); } else if(flag4==1) { printf("backward\n"); } else printf("fantasy\n"); } return 0;}/*aaaaaaabsdefasdefaaaaaaabcdedcb*/
- CodeForces 8A Train and Peter (简单模拟题)
- CodeForces 8A - Train and Peter
- 8A - Train and Peter
- Codeforces Beta Round #8 A. Train and Peter KMP
- 【String::find】#8 A. Train and Peter
- CF 8A Train and Peter
- cf 8a Train and Peter
- Codeforces Beta Round #8A Train and Peter (string的运用)
- CodeForces 613 A. Peter and Snow Blower(计算几何)
- CodeForces 613A Peter and Snow Blowe
- codeforces 613A Peter and Snow Blower
- CodeForces An express train to reveries(简单的模拟)
- codeforces 74B Train 贪心 简单模拟
- Codeforces Round #259 (Div. 2) A. Little Pony and Crystal Mine(简单模拟实现题)
- codeforces 283A - Cows and Sequence 简单数据结构模拟
- [codeforces] A. Grasshopper And the String 简单模拟
- Peter and Snow Blower CodeForces
- Codeforces Round #403 (Div. 2) A. Andryusha and Socks(简单模拟)
- 使用dbms_backup_restore包修改dbname及dbid
- github的常用命令/上传和下载代码
- php中检查服务器问题
- 模板 使用中注意的问题 template<class T>
- 父类子类静态块、游离块、构造方法的执行顺序
- CodeForces 8A Train and Peter (简单模拟题)
- MKNetworkKit简介
- IOS Foundation框架基本类库的使用
- 开始执行不调试按钮为灰色
- 面试-UML图
- UNICODE的MFC程序执行系统命令方法
- curl 模拟yeelink数据提交和查询
- 五大常用算法之三:贪心算法
- C语言基础系列--数据进制转换