Codeforces Round #306 (Div. 2) A. Two Substrings 水题
A. Two Substrings
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/550/problem/A
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
ABA
Sample Output
NO
HINT
题意
判断字符串是否存在不重合的AB和BA
题解:
虽然是一道傻逼题,但是被cha的人很多
我提供两组数据ABACCAB 和ABAB
这两组数据,来感受一下~
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** int main() { //test; string s; string s1; cin>>s; s1=s; int flag=0; int n=s.size(); for(int i=0;i<n-1;i++) { if(s1[i]=='B'&&s1[i+1]=='A') { flag=1,s1[i]='#',s1[i+1]='#'; break; } } if(flag==1) { for(int i=0;i<n-1;i++) { if(s1[i]=='A'&&s1[i+1]=='B') { cout<<"YES"<<endl; return 0; } } } s1=s; flag=0; for(int i=0;i<n-1;i++) { if(s1[i]=='A'&&s1[i+1]=='B') { flag=1,s1[i]='#',s1[i+1]='#'; break; } } if(flag==1) { for(int i=0;i<n-1;i++) { if(s1[i]=='B'&&s1[i+1]=='A') { cout<<"YES"<<endl; return 0; } } } cout<<"NO"<<endl; return 0; }