窝也不知道这叫不叫贪心 = =
窝想说这题看题面是裸的二分图匹配
Hungray敲上去。。
然后您就TLE了。。。。
实际上这题是有线性算法的。。因为它是个环。。
那么就只能是 → ← → ← 这样的情况才行 = =
那么连续的一段→ → →就可以直接合并了。。。
然后直接计算!
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int Nmax = 1e6 + 5;
string s;
int N, cnt;
bool Add[Nmax];
int main()
{
ios :: sync_with_stdio(false);
cin >> s; N = s.size();
for (int i = 0; i < N; ++ i) {
if (!i || s[i] ^ s[i - 1]) Add[++ cnt] = 1;
else Add[cnt] = 0;
}
if (s[0] == s[N - 1]) Add[1] = 0, -- cnt;
int temp = 0, ans = 0;
for (int i = 1; i <= cnt; ++ i) {
if (Add[i]) ++ temp;
else ans += (temp >> 1) + 1, temp = 0;
}
cout << ans + (temp >> 1) << endl;
return 0;
}