• A


    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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 Input

    Input
    ABA
    Output
    NO
    Input
    BACFAB
    Output
    YES
    Input
    AXBYBXA
    Output
    NO

    Hint

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

    题意:

    给定一字符串,求能否找出“AB”“BA”两不重叠字符串。

    啊啊啊,坑比的字符串题!!卡了三组数据TAT

    附AC代码:

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int main(){
     6     string s;
     7     int t=-1,v=-1,ans,temp,x,y,a,b,c,d;
     8     cin>>s;
     9     int len=s.size();
    10     ans=0;
    11     temp=0;
    12     x=0;
    13     y=0;
    14     for(int i=0;i<len;i++){
    15         if(s[i]=='A'&&s[i+1]=='B'){
    16             if(i!=t&&!ans){
    17                 ans++;
    18                 t=i+1;
    19                 a=i;
    20                 break;
    21             }
    22         }
    23     }
    24     for(int i=0;i<len;i++){
    25         if(s[i]=='B'&&s[i+1]=='A'){
    26             if(i!=t&&i+1!=a&&!temp){
    27                 temp++;
    28                 t=i+1;
    29                 break;
    30             }    
    31         }
    32     }
    33     for(int i=0;i<len;i++){
    34         if(s[i]=='B'&&s[i+1]=='A'){
    35             if(i!=v&&!x){
    36                 x++;
    37                 v=i+1;
    38                 b=i;
    39                 break;
    40             }    
    41         }
    42     }
    43     for(int i=0;i<len;i++){
    44         if(s[i]=='A'&&s[i+1]=='B'){
    45             if(i!=v&&i+1!=b&&!y){
    46                 y++;
    47                 v=i+1;
    48                 break;
    49             }
    50         }
    51     }
    52     if(ans&&temp){
    53         cout<<"YES"<<endl;
    54         return 0;
    55     }
    56     else if(x&&y){
    57         cout<<"YES"<<endl;
    58         return 0;
    59     }
    60     cout<<"NO"<<endl;
    61     return 0;
    62 } 
  • 相关阅读:
    2022.05.31软件更新公告
    为什么不网购方便面
    js localStorage
    uniapp上拉加载更多功能的简单实现
    Centos 系统 软raid 0 实现
    使用vscode调试ros
    wget 命令用法
    是否应该提前还房贷
    mybatis generator 自动生成的拼装sql分析
    动态样式+模板字符串
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/5720419.html
Copyright © 2020-2023  润新知