求助,#3,#6Wrong了,改了两次没过
查看原帖
求助,#3,#6Wrong了,改了两次没过
409288
NakatoYuki楼主2022/10/4 20:12

暴力搜索的写法。 以搜左边为例,能进入循环体的要求 1.没搜重复(即没有搜了一圈) 2.第一个搜到的字符(第一个不用管是什么) 3.搜到和第一个不是'w'的字符一样的字符 4.搜到'w' 右边同理

#include <iostream>
using namespace std;
const int N = 360;
char necklace[N];
int n;

int main()
{
    scanf("%d%s",&n, necklace);
    int ans = 0;
    
    for(int i = 0; i < n - 1; i++) //断边
    {
        int l = i, r = (i + 1) % n;
        int res = 0;

        char ch = 'w';
        bool flag = false;
        while(l == i || necklace[l] == necklace[(l + 1) % n] || necklace[l] == 'w' || (necklace[l] == ch && ch != 'w'))
        {
            if(necklace[l] != 'w') ch = necklace[l];
            ++res;
            l = (l - 1 + n) % n;
            if(l == i)
            {
                flag = true;
                break;
            }
        }

        ch = 'w';
        while(!flag && (r == (i + 1) % n || necklace[r] == necklace[(r - 1 + n) % n] || necklace[r] == 'w'|| (necklace[r] == ch && ch != 'w')))
        {
            if(necklace[r] != 'w') ch = necklace[r];
             ++res;
             r = (r + 1) % n;
        }

        ans = max(ans, res);
    }

    printf("%d\n", ans);
    return 0;
}
2022/10/4 20:12
加载中...