跳到正文

题解

题解:P16815 [蓝桥杯 2026 国 Python B] 糖果转盘

oi_wiki约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月6日
更新
2026年6月26日
篇幅
约 2 分钟
题目
P16815

版本

  1. v1当前

题意

环形字符串 SS,选一个位置切开拉成链。若链中任意相邻字符不同,则该切口合法。求合法切口数。

思路

统计环中相邻相同字符对数 cntcnt:

  • cnt=0cnt=0:任意切都合法,答案为 NN
  • cnt=1cnt=1:只能切在该处,答案为 11
  • cnt≥2cnt\ge 2:一刀无法消除所有相同相邻,答案为 00
AC Code
C++
#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;
}
原文