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 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.
Sample Input
atob
a
b
Sample Output
forward
Hint
题意
有一个人在坐火车,不知道从A到B的还是从B到A的
现在他迷迷糊糊睡了一觉,然后醒来,写下来他看到的字符,然后又迷迷糊糊睡了下去,然后又写下他看到的字符
现在给你这个从A到B的字符串,问这个人坐的火车从A到B还是从B到A的
题解:
首先正着做,暴力求KMP,求匹配的位置,如果存在s1串和s2串匹配的位置不重合的话,就说明合法
正着做一次,再反着做一次就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
char s[maxn],s1[maxn],s2[maxn];
int p[maxn];
vector<int>p1;
vector<int>p2;
int main()
{
scanf("%s",s+1);
scanf("%s",s1+1);
scanf("%s",s2+1);
int len=strlen(s1+1);
int j=0;
for(int i=2;i<=len;i++)
{
while(j>0&&s1[j+1]!=s1[i])j=p[j];
if(s1[j+1]==s1[i])j++;p[i]=j;
}
int len2=strlen(s+1);
j=0;
for(int i=1;i<=len2;i++)
{
while(j>0&&s1[j+1]!=s[i])j=p[j];
if(s1[j+1]==s[i])j++;
if(j==len){p1.push_back(i),j=p[j];}
}
len = strlen(s2+1);
j=0;
memset(p,0,sizeof(p));
for(int i=2;i<=len;i++)
{
while(j>0&&s2[j+1]!=s2[i])j=p[j];
if(s2[j+1]==s2[i])j++;p[i]=j;
}
j=0;
for(int i=1;i<=len2;i++)
{
while(j>0&&s2[j+1]!=s[i])j=p[j];
if(s2[j+1]==s[i])j++;
if(j==len){p2.push_back(i),j=p[j];}
}
sort(p1.begin(),p1.end());
sort(p2.begin(),p2.end());
int flag1 = 0,flag2 = 0;
if(p1.size()!=0&&p2.size()!=0)
if(p1[0]+strlen(s2+1)<=p2[p2.size()-1])flag1=1;
p1.clear(),p2.clear();
reverse(s+1,s+1+len2);
memset(p,0,sizeof(p));
len=strlen(s1+1);
j=0;
for(int i=2;i<=len;i++)
{
while(j>0&&s1[j+1]!=s1[i])j=p[j];
if(s1[j+1]==s1[i])j++;p[i]=j;
}
len2=strlen(s+1);
j=0;
for(int i=1;i<=len2;i++)
{
while(j>0&&s1[j+1]!=s[i])j=p[j];
if(s1[j+1]==s[i])j++;
if(j==len){p1.push_back(i),j=p[j];}
}
len = strlen(s2+1);
j=0;
memset(p,0,sizeof(p));
for(int i=2;i<=len;i++)
{
while(j>0&&s2[j+1]!=s2[i])j=p[j];
if(s2[j+1]==s2[i])j++;p[i]=j;
}
j=0;
for(int i=1;i<=len2;i++)
{
while(j>0&&s2[j+1]!=s[i])j=p[j];
if(s2[j+1]==s[i])j++;
if(j==len){p2.push_back(i),j=p[j];}
}
sort(p1.begin(),p1.end());
sort(p2.begin(),p2.end());
if(p1.size()!=0&&p2.size()!=0)
if(p1[0]+strlen(s2+1)<=p2[p2.size()-1])flag2=1;
if(flag1&&flag2)
return puts("both"),0;
if(flag1)
return puts("forward"),0;
if(flag2)
return puts("backward"),0;
return puts("fantasy"),0;
}
Codeforces Beta Round #8 A. Train and Peter KMP的更多相关文章
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(2)
我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...
- Thinkphp的自定义路由(route.php)
废话:因为thinkphp的默认路由会导致URL特别长,从而会影响搜索引擎优化.所以就衍生了自定义路由,尽量将URL缩短. 这是默认的路由文件: <?php return [ '__patter ...
- 不相交集ADT--数组实现
不相交集是解决等价问题的一种有效的数据结构,之所以称之为有效是因为,这个数据结构简单(几行代码,一个简单数组就可以搞定),快速(每个操作基本上可以在常数平均时间内搞定). 首先我们要明白什么叫做等价关 ...
- python基础===利用unittest进行测试用例执行的几种方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而tests ...
- 连接数据库:ERROR:The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration prop
本打算在maven项目中配置mybatis试试看,想到mybatis如果不是在容器中运行,那么他的事务控制实际上可以使用的是jdbc的提交和回滚,这就要在pom.xml文件中配置mysql-conne ...
- xpath简单应用
相对路径与绝对路径: 如果"/"处在XPath表达式开头则表示文档根元素,(表达式中间作为分隔符用以分割每一个步进表达式)如:/messages/message/subject是一 ...
- (二)Spring 之IOC 详解
第一节:spring ioc 简介 IOC(控制反转:Inversion of Control),又称作依赖注入dependency injection( DI ),是一种重要的面向对象编程的法则来削 ...
- 庆祝团队合著的《自主实现SDN虚拟网络与企业私有云》终于得以出版 --- 本人负责分布式存储部分的编写
https://item.jd.com/12154254.html 京东购买地址
- 面试题15:链表中倒数第K个节点
类似问题:判断是否是环形节点,寻找中间节点,一个指针一次走一步,另一个指针一次走两步 注意检查三点鲁棒性: 1. 链表为空 2. k为0 3. 链表长度不够k ListNode* FindKthToT ...
- Ubuntu编译安装nginx,php,mysql
摘要: 整理的Ubuntu编译安装nginx,php,mysql的步骤,主要来自对驻云的sh-1.4.1中脚本的整理,随时代进步,内容中的软件或者命令请自行更新 目录准备 创建用户 userdel w ...