题意
环形字符串 ,选一个位置切开拉成链。若链中任意相邻字符不同,则该切口合法。求合法切口数。
思路
统计环中相邻相同字符对数 :
- :任意切都合法,答案为
- :只能切在该处,答案为
- :一刀无法消除所有相同相邻,答案为
AC Code
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int N;
string S;
cin >> N >> S;
int cnt = 0;
for (int i = 0; i < N - 1; ++i)
if (S[i] == S[i + 1])
cnt++;
if (S[N - 1] == S[0])
cnt++;
if (cnt == 0)
cout << N << "\n";
else if (cnt == 1)
cout << "1\n";
else
cout << "0\n";
return 0;
}