codeforces #306 div 2 A

A - Two Substrings
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

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

这题竟然wa了四次...

我发现如果最开始做一道题没想清楚的话...即使过了若干天后重新想..也特别容易wa...?

我们可以直接考虑覆盖概率最小的情况.就是最边缘.

分为两种情况,一种是xxxxABxxxxBAxxxx

一中是xxxxBAxxxxxABxxxxx

注意找到记录的一定是最边缘的位置!找到相应的break掉也好加个判断也好...

 1 /*************************************************************************
 2     > File Name: code/cf/#306/A.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月22日 星期二 18时11分04秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 string st;
32 int len;
33 int main()
34 {
35   #ifndef  ONLINE_JUDGE 
36    freopen("in.txt","r",stdin);
37   #endif
38    while (cin>>st){
39    len = st.length();
40    int  p  = -1 ;
41    int k[10]={};
42    ms(k,-1);
43    for ( int i = 0 ; i <len-1  ; i++)
44     {
45     if (st[i]=='A'&&st[i+1]=='B'&&k[0]==-1)
46     {
47         k[0] = i;
48     }
49     if (st[i]=='B'&&st[i+1]=='A'&&k[2]==-1)
50     {
51         k[2] = i;
52     }
53     }
54    for ( int i = len - 1; i >= 1 ; i--)
55     {
56     if (st[i]=='A'&&st[i-1]=='B'&&k[1]==-1)
57     {
58         k[1] = i;
59     }
60     if (st[i]=='B'&&st[i-1]=='A'&&k[3]==-1)
61     {
62         k[3] = i;
63     }
64     }
65    if ((k[0]>=0&&k[1]>=0&&abs(k[1]-k[0])>2)||(k[2]>=0&&k[3]>=0&&abs(k[3]-k[2])>2))
66     {
67     puts("YES");
68     }
69     else
70     {
71     puts("NO");
72     }
73    }
74     
75    
76  #ifndef ONLINE_JUDGE  
77   fclose(stdin);
78   #endif
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/111qqz/p/4851410.html