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; }

最新文章

  1. 无需activity获得屏幕尺寸
  2. json_encode中文unicode的问题
  3. JAVA线程同步辅助类CyclicBarrier循环屏障
  4. socket基本
  5. innodb数据库批量转换表引擎为MyISAM
  6. Nagios全方位告警接入-电话/微信/短信都支持
  7. iOS 3DES DES AES加密注意事项!!很重要,否则会加密失败
  8. windows身份验证模式和SQL server身份验证模式 有什么不同
  9. 【Android Developers Training】 106. 创建并检测地理围栏
  10. RSA算法介绍
  11. JDK源码分析-String、StringBuilder、StringBuffer
  12. 《http权威指南》读书笔记12
  13. curl模拟post json或post xml文件
  14. EF实现增删改查
  15. python + docker, 实现天气数据 从FTP获取以及持久化(五)-- 利用 Docker 容器化 Python 程序
  16. 超级简单的jquery轮播图demo
  17. PHP数据库类
  18. 『ACM C++』 PTA 天梯赛练习集L1 | 050-51
  19. JavaScript -- 节点操作, 事件触发, 表单伸缩
  20. Ubuntu中vi命令的使用

热门文章

  1. 190.Reverse Bits---位运算
  2. git中如何查看一个文件的修改(更新)历史
  3. nginx 服务器篇
  4. 一个带重试次数的curl 函数
  5. AXFR和IXFR区域传输及原理
  6. JQuery - 特殊字符转义(Uncaught Error: Syntax error, unrecognized expression:的处理)
  7. appium+python自动化39-adb shell输入中文(ADBKeyBoard)
  8. TCP和UDP发送数据包的大小问题
  9. java总结(方法与对象)
  10. vscode debugger for chrome 调试webpack的配置问题