A - Two Substrings

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 } 
时间: 2024-11-05 06:10:02

A - Two Substrings的相关文章

[Leetcode] DP-- 467. Unique Substrings in Wraparound String

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

uva 10829 - L-Gap Substrings(后缀数组)

题目链接:uva 10829 - L-Gap Substrings 题目大意:给定一个字符串,问有多少字符串满足UVU的形式,要求U非空,V的长度为g. 解题思路:对字符串的正序和逆序构建后缀数组,然后枚举U的长度l,每次以长度l分区间,在l和l+d+g所在的两个区间上确定U的最大长度. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using nam

[LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

POJ 3415 Common Substrings (求长度不小于k的公共子串的个数)

Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10002   Accepted: 3302 Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given two strings A, B and one integer K, we define S,

字符串(后缀数组):POJ 3415 Common Substrings

Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given two strings A, B and one integer K, we define S, a set of triples (i, j, k): S = {(i, j, k) | k≥K, A(i, k)=B(j, k)}. You are to give

SPOJ 题目694 Distinct Substrings(后缀数组,求不同的子串个数)

DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first li

SPOJ 705 New Distinct Substrings

New Distinct Substrings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: SUBST164-bit integer IO format: %lld      Java class name: Main Given a string, we need to find the total number of its distinct subst

CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= = 因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #i

467. Unique Substrings in Wraparound String

https://leetcode.com/problems/unique-substrings-in-wraparound-string/#/description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefgh