550A Two Substrings
问题
分析
代码
/*
Dreams never shine!
It's you that shine while chasing your dreams :)
JAYO!!
大佬带带我吧
Master, Teach Me!
*/
#include <iostream>
#include <string>
using namespace std;
string s;
void Solve() {
cin >> s;
int x1 = s.find("AB"); // FIRST OCCURENCE OF AB IS X1
int x2 = s.find("BA", x1 + 2); // OCCURENCE OF BA FROM X1+2 POSITION
int y1 = s.find("BA");
int y2 = s.find("AB", y1 + 2);
if ((x1 != -1 && x2 != -1) || (y1 != -1 && y2 != -1)) {
cout << "YES" << endl;
}
else cout << "NO" << endl;
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
Solve();
return 0;
}
另一份代码
/*
Dreams never shine!
It's you that shine while chasing your dreams :)
JAYO!!
*/
#include <iostream>
#include <string>
using namespace std;
void Solve() {
string s; cin >> s;
int n = s.size(), i;
bool AB = false, BA = false;
for (i = 1; i < n; i++) {
if (s[i - 1] == 'A' && s[i] == 'B') {
i = i + 1;
AB = true;
break;
}
}
if (!AB) {
cout << "NO\n"; return ;
}
for (; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'A') {
cout << "YES\n"; return ;
}
}
AB = false; BA = false;
for (i = 1; i < n; i++) {
if (s[i - 1] == 'B' && s[i] == 'A') {
i = i + 1;
BA = true;
break;
}
}
if (!BA) {
cout << "NO\n"; return ;
}
for (; i < n - 1; i++) {
if (s[i] == 'A' && s[i + 1] == 'B') {
cout << "YES\n"; return ;
}
}
cout << "NO\n";
}
int main()
{
Solve();
return 0;
}
小结论
离不开书本系统的练习…
好好结合一下书本!!!