550A Two Substrings

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;
}


小结论

离不开书本系统的练习…
好好结合一下书本!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值