CodeForces 1569A Balanced Substring

题目链接:CodeForces 1569A Balanced Substring

题目大意:
给定一个由(a)(b)组成的字符串,找到一个子串使其中(a)(b)字符的数量相等。

题解:
在字符串中(a)(b)字符的交界处取一个长度为(2)的子串,则其中(a)(b)字符的数量均为(1),满足条件。

#include <iostream>
#include <string>
using namespace std;
 
string s;
int t, n, l;
 
int main() {
    cin >> t;
    while (t--) {
        cin >> n >> s;
        l = 0;
        while (l + 1 < n && s[l] == s[l + 1]) l++;
        if (l + 1 == s.length()) cout << -1 << ' ' << -1 << endl;
        else cout << l + 1 << ' ' << l + 2 << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/IzumiSagiri/p/15249469.html