#include <iostream>
#include <queue>
#include <string>
#include <cctype>
using namespace std;
int BFS(const string& s) {
int n = s.length();
queue<int> q;
for (int i = 0; i <= n - 3; i++) {
q.push(i);
}
while (!q.empty()) {
int pos = q.front();
q.pop();
if (s[pos] == 'b' && s[pos + 1] == 'o' && s[pos + 2] == 'b') {
return pos;
}
}
return -1;
}
int main() {
string S;
cin >> S;
for (auto& c : S) {
c = tolower(c);
}
cout << BFS(S);
}
看到这个题目的题干和BFS有关系,虽然本题解法和BFS一点关系都没有:(
由于我没有看到用BFS解题的答案,所以决定用BFS的样子做一个线性搜索。
我们先开一个队列,循环n-2次,然后把三个三个对应即可。

京公网安备 11010502036488号