題意 : 是否能在題目給你的string中找到一個AB和BA , 順序沒差
解法 : 因為ABA或BAB可以同時為AB或BA , 所以可以有三種case
1. ABA/BAB 配 AB
2. ABA/BAB 配 BA
3. BA 配 AB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
using namespace std; | |
int main() | |
{ | |
char a[100000]; | |
cin >> a; | |
int l = strlen(a); | |
int flag1 = 0 ,flag2 = 0 ,flag3 = 0 ,flag18 = 0 ; | |
for(int i = 0 ; i < l ; ) | |
{ | |
int k = i ; | |
if(((a[i] == 'A' && a[i+1] == 'B' && a[i+2] == 'A') || (a[i] == 'B' && a[i+1] == 'A' && a[i+2] == 'B') ) && i <= l-3){flag1 = 1 ; i = i+3 ; } | |
if(a[i] == 'A' && a[i+1] == 'B' && i <= l-2) { flag2 = 1 ; i = i+2 ; } | |
if(a[i] == 'B' && a[i+1] == 'A' && i <= l-2) { flag3 = 1 ; i = i+2 ; } | |
if(k == i) i++; | |
if(flag1 == 1 && flag2 == 1) flag18 = 1 ; | |
if(flag1 == 1 && flag3 == 1) flag18 = 1 ; | |
if(flag2 == 1 && flag3 == 1) flag18 = 1 ; | |
} | |
if(flag18 == 1) cout << "YES" << endl ; | |
else cout << "NO" << endl ; | |
} |
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。