• Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】


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

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

    【分析】:strstr函数判断AB和BA。

    strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

    【代码】:

    #include<bits/stdc++.h>
    using namespace std;
    char str[200000],*p;
    int main()
    {
        cin>>str;
        if( (p=strstr(str,"AB")) && (strstr(p+2,"BA")) ) puts("YES");
        else if( (p=strstr(str,"BA")) && strstr(p+2,"AB") ) puts("YES");
        else puts("NO");
        return 0;
    }
    View Code
  • 相关阅读:
    开源的excel读取库libxls在windows下的编译,且支持中文,全网首发
    UTF8与GBK、GB2312等其他字符编码的相互转换
    深入理解Netty框架
    论一道编程面试题解法
    JVM调优总结-Xms -Xmx -Xmn -Xss
    Linux上安装jdk1.8和配置环境变量
    .NET GC垃圾回收器
    T4 模板
    Vue 环境搭建
    Spring Cloud 入门系列(一)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7953907.html
Copyright © 2020-2023  润新知