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的更多相关文章
随机推荐
- 💈 线程间互访助手类 (EN)
Conmajia © 2012, 2018 Published on August 5th, 2012 Updated on February 2nd, 2019 Introduction While ...
- Spring Cloud Alibaba基础教程:使用Sentinel实现接口限流
最近管点闲事浪费了不少时间,感谢网友libinwalan的留言提醒.及时纠正路线,继续跟大家一起学习Spring Cloud Alibaba. Nacos作为注册中心和配置中心的基础教程,到这里先告一 ...
- IIS安装以及发布
控制面板-所有控制面板项-程序和功能-打开或关闭windows功能-Internet 信息服务,里面全部打钩点击确实.安装完成. 控制面板-管理工具-Internet 信息服务管理器-双击打开 ...
- 基于Springboot集成security、oauth2实现认证鉴权、资源管理
1.Oauth2简介 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OAu ...
- Poj1543
Perfect Cubes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16522 Accepted: 8444 De ...
- python 正则验证 IP地址与MAC地址
#coding=utf-8 import re def isValidIp(ip): if re.match(r"^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} ...
- Django之模板系统
变量的使用: def test(request): num=1 s='hello' li=[1,2,['a','b']] dic={'name':'w','age':1} se={1,2,3} tup ...
- DVWA 黑客攻防演练(十四)CSRF 攻击 Cross Site Request Forgery
这么多攻击中,CSRF 攻击,全称是 Cross Site Request Forgery,翻译过来是跨站请求伪造可谓是最防不胜防之一.比如删除一篇文章,添加一笔钱之类,如果开发者是没有考虑到会被 C ...
- 智能ERP收银统计-优惠统计计算规则
1.报表统计->收银统计->优惠统计规则 第三方平台优惠:(堂食订单:支付宝口碑券优惠)+(外卖订单:商家承担优惠) 自平台优惠:(堂食订单:商家后台优 ...
- 关于LeetCode上链表题目的一些trick
最近在刷leetcode上关于链表的一些高频题,在写代码的过程中总结了链表的一些解题技巧和常见题型. 结点的删除 指定链表中的某个结点,将其从链表中删除. 由于在链表中删除某个结点需要找到该结点的前一 ...