codeforces8A
Train and Peter
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.
Examples
atob
a
b
forward
aaacaaa
aca
aa
both
Note
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.
sol:直接字符串哈希后暴力匹配即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef unsigned long long ull;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,Power=;
int n,n1,n2;
char S[N],S1[N],S2[N];
ull Bin[N],Hash[N],H1=,H2=;
inline void Pre(char *S)
{
int i;
for(i=;i<=n;i++)
{
Hash[i]=Hash[i-]+Bin[i]*(S[i]-'a');
}
}
inline ull Ask(int l,int r)
{
return (Hash[r]-Hash[l-]);
}
inline bool Solve()
{
int i,Last=;
bool Flag=;
for(i=n1;i<=n;i++)
{
if(!Flag)
{
if(Ask(i-n1+,i)==H1*Bin[i-n1]) Flag=; Last=i;
}
else if(i-Last>=n2)
{
if(Ask(i-n2+,i)==H2*Bin[i-n2]) return true;
}
}
return false;
}
int main()
{
int i;
bool ans1=,ans2=;
scanf("%s%s%s",S+,S1+,S2+);
n=strlen(S+); n1=strlen(S1+); n2=strlen(S2+);
Bin[]=;
for(i=;i<=n;i++)
{
Bin[i]=Bin[i-]*Power;
}
for(i=;i<=n1;i++) H1=H1+Bin[i]*(S1[i]-'a');
for(i=;i<=n2;i++) H2=H2+Bin[i]*(S2[i]-'a');
Pre(S);
ans1=Solve();
reverse(S+,S+n+);
Pre(S);
ans2=Solve();
if(ans1)
{
if(!ans2) puts("forward");
else puts("both");
}
else if(ans2)puts("backward");
else puts("fantasy");
return ;
}
/*
Input
atob
a
b
Output
forward Input
aaacaaa
aca
aa
Output
both
*/
codeforces8A的更多相关文章
随机推荐
- SmartSql 快速使用指南
SmartSql 快速使用指南(https://github.com/Ahoo-Wang/SmartSql) ISmartSqlMapper 常用(部分)接口概述 函数 说明 Execute IDbC ...
- select标签 禁止选择但又能通过序列化form表单传值到后台
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁止选择但又能通过序列化form表单传值到后台,但是当我们使用disabled="disabled"时发现,无法序列化 ...
- Centos7配置MongoDB以及端口修改
一.安装之前可以参考官网安装教程: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 二.登录服务器,配置yum ...
- js中let和var的区别 不懂得加QQ 2270312758
js中使用let定义变量的时候,是需要使用严格模式的,我看到网上有的博客说:如果在不使用严格模式的情况下,使用let会报错,但是在实验的过程中,我直接定义了let变量而且也没有使用严格模式,并没有报任 ...
- 结合JDK源码看设计模式——策略模式
前言: 现在电商已经成为我们生活中不可或缺的购物渠道,同时各大商家会针对不同的时间做出不同的折扣,这在我们看来就是一种营销手段,也是一种策略,今天我们就来讲讲JDK中的策略模式是怎么样的. 一.定义 ...
- MyBatis之整合Spring
MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...
- js中关于两个变量的比较
今天在匆忙的写代码过程中,测试突然跑过来跟我说,有一个小的bug,但是不能完全的复现,但确实存在.需要我们这边参与查找.首先来说一下我们这个的业务逻辑,其实非常的简单,就是一个开房间的后台,里面有一项 ...
- 3.JavaScript-语法、关键保留字及变量
语法构成区分大小写标识符注释直接量字面量literal关键字保留字变量语法构成JavaScript 的语言核心 ECMAScript. 区分大小写ECMAScript 中的一切,包括变量.函数名和操作 ...
- 网页三剑客:HTML+CSS+JavaScript 之JavaScript
JavaScript 简介 JavaScript 是互联网上最流行的脚本语言,这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. JavaScrip ...
- 基于python的种子搜索网站-项目部署
本讲会对种子搜索网站的部署过程进行详细的讲解. 网站演示: https://bt.mypython.me 源码地址: https://github.com/geeeeeeeek/bt 项目部署过程 系 ...