zoukankan      html  css  js  c++  java
  • Codeforces Round #306 (Div. 2)——A——Two Substrings

    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".

     智商被压制。。被hack了,只要倒着再判断一下就行

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {
        char a[100110];
        while(~scanf("%s",&a)){
            int n = strlen(a);
            int t1 ,t2 ;
            t1 = t2 = 0;
            int i = 0;
            while(i < n){
                if(a[i] == 'A' && a[i+1] == 'B'&&t1 == 0){
                t1++;
                i++;}
                else if(a[i] == 'B' && a[i+1] == 'A'&&t2 == 0){
                t2++;
                i++;
                }
                i++;
            }
            if(t1 > 0 && t2 > 0){
                printf("YES
    ");
                continue;
            }
             i = n-1;
            t1 = t2 = 0;
            while(i >= 0){
                if(a[i] == 'A' && a[i+1] == 'B'&& t1 == 0){
                    t1++;
                    i--;
                }
                else if(a[i] == 'B' && a[i+1] == 'A' && t2 == 0){
                    t2++;
                    i--;
                }
                i--;
            }
            if(t1 > 0 && t2 > 0)
            printf("YES
    ");
            else printf("NO
    ");
            }
        return 0;
    }
    

      

  • 相关阅读:
    托管和使用WCF服务:WAS(Windows激活服务)
    突发的灵感
    C# 常见图像处理效果
    C# WinForm TreeView 递归选择父节点和子节点
    C# WinForm ComboBox 枚举 选定值
    C# Socket 异步 UDP
    C# WinForm 判断窗体控件是否修改过
    C# 线程同步 信号量 Semaphore
    C# WinForm ComboBox Items 选定值
    C# WinForm ComboBox 自定义数据项 (ComboBoxItem )
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4555076.html
Copyright © 2011-2022 走看看