题解:P16815 [蓝桥杯 2026 国 Python B] 糖果转盘
P16815 糖果转盘 题解
题意简述
环形字符串,在相邻字符间切一刀展开为线性串。若展开后任意相邻字符均不相同,则该切口合法,求合法切口总数。
解题思路
环形结构中,切口仅切断当前这一组相邻关系,其余相邻关系全部保留。
统计环形中相邻且字符相等的对数
C++ 代码
# include <bits/stdc++.h>
using namespace std;
long long n,cnt = 0;
string s;
int main () {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> s;
s += s;
for (int i = 0;i < n;i++) {
if (s[i] == s[i + 1]) {
cnt ++;
}
}
if (cnt == 0) cout << n;
else if (cnt == 1) cout << 1;
else cout << 0;
return 0;
}
复杂度
时间复杂度
AI 使用声明
核心思路与代码为本人原创,仅使用 AI 进行排版优化。