Codeforces 550A substrings (analog)

Source: Internet
Author: User

"Topic link" click here~~
"To the effect of the topic":
You are given string s. Your task is to determine if the given string s contains the 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 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains the non-overlapping substrings "AB" and "BA", and "NO" otherwise.

Sample Test (s)
Input
ABA
Output
NO
Input
Bacfab
Output
YES
Gives a line of characters to determine if there are two non-overlapping strings "AB" and "BA"
Code:

The /*str.find () function returns the position of the first occurrence of STR in the string, starting with index. Returns String::npos if not found, returns the position where Str first appears in the string (from index, length). Returns String::npos if not found, returns the position of the first occurrence of the character ch in the string (from index). If not found, return to String::npos * *#include <bits/stdc++.h>using namespace STD;stringStrintMain () {Cin>>str;intLen=str.size ();if(len<=3)puts("NO");Else{intA=str.find ("AB");intB=str.find ("BA", A +2);intC=str.find ("BA");intD=str.find ("AB", c+2);if(a!=-1&&b!=-1|| c!=-1&&d!=-1)puts("YES");Else puts("NO"); }return 0;}/*strstr function: Find where the string first appears */#include <bits/stdc++.h>using namespace STD;Charstr[200000],*p;intMain () {Cin>>str;if((p=strstr(STR,"AB")) && (strstr*pi2,"BA")))puts("YES");Else if((p=strstr(STR,"BA")) &&strstr*pi2,"AB"))puts("YES");Else puts("NO");return 0;}

Codeforces 550A substrings (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.