Codeforces Beta Round #8A Train and Peter (string的运用)

Train and Peter

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on CodeForces.
Original ID: 8A

64-bit integer IO format: %I64d     
Java class name: (Any)

Prev

Submit Status Statistics Discuss

Next

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

Input

atobab

Output

forward

Input

aaacaaaacaaa

Output

both

Hint

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.

题意:输入三个字符串,s,a,b,s为源串,a,b为目标串,问是否可以依次得到a串和b串,或相反.

此题本来可以用字符串模拟的,但是C++有强大的string类,怎么能不用呢?

看了q神的代码,"佩服"两字油然而生!

顺便补一下相关的函数:

查找(find)

语法:

size_type find( const basic_string &str, size_type index );

size_type find( const char *str, size_type index );

size_type find( const char *str, size_type index, size_type length );

size_type find( char ch, size_type index );

find()函数:

返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,

返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,

返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

例如:

string str1( "Alpha Beta Gamma Delta" );
unsigned int loc = str1.find( "Omega", 0 );
if( loc != string::npos )
    cout << "Found Omega at " << loc << endl;
else
    cout << "Didn't find Omega" << endl;//print this

AC代码:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    string s,a,b;
    while(cin>>s>>a>>b)
    {
        bool isok[2]= {0,0};
        for(int i=0; i<2; i++)
        {
            int idx=s.find(a);
            if(idx!=string::npos)
            {
                idx=s.find(b,idx+a.size());
                if(idx!=string::npos)
                    isok[i]=1;
            }
            reverse(s.begin(),s.end());
        }
        if(isok[0]&&isok[1])
            cout<<"both"<<endl;
        else if(isok[0])
            cout<<"forward"<<endl;
        else if(isok[1])
            cout<<"backward"<<endl;
        else
            cout<<"fantasy"<<endl;
    }
    return 0;
}
时间: 2024-10-22 04:50:02

Codeforces Beta Round #8A Train and Peter (string的运用)的相关文章

codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)

题目链接:http://www.codeforces.com/problemset/problem/118/A题意:字符串转换……C++代码: #include <string> #include <iostream> using namespace std; string s; bool _in(char c, string s) { for (int i =0 ; i < s.length(); i ++) if (c == s[i]) return true; retu

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

Codeforces Beta Round #1 B. Spreadsheets

Codeblocks坏掉了,我不知道该怎么修,只能过两天重装系统了. 没办法.这个题是用Java写的,代码风格不好不要骂我~~ 题目大意: Excel表格那种坐标系统,和正常的坐标系统.用代码实现转换. 就是模拟题啊,代码量比较小. 下面是代码: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); i

Codeforces Beta Round #1 C. Ancient Berland Circus

果然Java还是不靠谱啊,一个NaN把我整了半天~~ 题目大意: 有一个正多边形,给出任意三个顶点的坐标,求这个正多边形的最小面积. 解题思路: 首先要知道这三个顶点组成的三角形的外接圆一定是这个正多边形的外接圆. 用过计算出三角形的三边长,可以计算出三角型面积,进而推出外接圆半径. 可以得到三个圆心角,找出最大公约数,那就是最大角度. 就可以计算出多边形面积了~~ 下面是代码: import java.text.DecimalFormat; import java.util.Scanner;

水题 Codeforces Beta Round #70 (Div. 2) A. Haiku

题目传送门 1 /* 2 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 3 下午网速巨慢:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 cons

Codeforces Beta Round#2

Codeforces Beta Round#2 http://codeforces.com/contest/2 A 模拟题 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 map<string,ll>mp; 6 struct sair{ 7 string str; 8 int id; 9 ll num; 10 }a[1005]; 11 12 bool cmp(sair a,sa

Codeforces Beta Round #3

Codeforces Beta Round #3 http://codeforces.com/contest/3 A 找规律题.但我懒得找,直接暴搜 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*

Codeforces Beta Round #5

Codeforces Beta Round #5 http://codeforces.com/contest/5 A 模拟题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLI

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef