zoukankan      html  css  js  c++  java
  • Two Substrings

    Two Substrings

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).

    Input

    The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.

    Output

    Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.

    Sample test(s)
    input
    ABA
    output
    NO
    input
    BACFAB
    output
    YES
    input
    AXBYBXA
    output
    NO
    Note

    In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".

    In the second sample test there are the following occurrences of the substrings: BACFAB.

    In the third sample test there is no substring "AB" nor substring "BA".

    正搜一边,反搜一边。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char ch[100005];
    int main(){
        bool flag1 = 0;
        bool sign1 = 0;
        bool flag2 = 0;
        bool sign2 = 0;
        scanf("%s",&ch);
        int len = strlen(ch);
        for(int i = 0; i < len; i++){
            if(!flag1 && ch[i] == 'A' && ch[i+1] == 'B'){
                flag1 = 1;
                i++;
            }
            else if(!sign1 && ch[i] == 'B' && ch[i+1] == 'A'){
                sign1 = 1;
                i++;
            }
        }
        for(int i = len-1; i >= 0; i--){
            if(!sign2 && ch[i] == 'A' && ch[i-1] == 'B'){
                sign2 = 1;
                i--;
            }
            else if(!flag2 && ch[i] == 'B' && ch[i-1] == 'A'){
                flag2 = 1;
                i--;
            }
        }
        if((sign1 && flag1) || (sign2 && flag2)){
            puts("YES");
        }
        else puts("NO");
        return 0;
    }
  • 相关阅读:
    Prometheus监控学习笔记之Prometheus监控简介
    GO语言学习笔记之Linux环境下安装GO语言
    Jetson tx1 安装ROS
    ssh Jetson tk1
    Jetson tk1 安装 Intel 7260 无线网卡驱动
    ubuntu 14.04 软件中心闪退解决方案
    Jetson tk1 刷机后要做的几件事
    Jetson tk1 安装 usbtoserials 驱动(重新刷机)
    usbserials
    ubuntu 14.04 安装 eclipse
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4858039.html
Copyright © 2011-2022 走看看