[thinking] codeforces 1025c placement zebra

codeforces 1025C

Question meaning: for a string containing only w and b, you can perform clipping and flipping operation bw|bbw - > W b wbwbb. There is no limit to the number of times. What is the maximum number of wb cross subsequences that can be generated?

It's a question without any thought ~ ~! Reference blog

The problem is the bw (head) | (tail) bbw, generating wb| (tail) bbw, moving the latter section to the front is wbbwb, and then this is bwbbw (original string). If we regard this string as a ring connected end-to-end, the relative position of these letters will not change no matter how we operate. That is to say, we can directly count the longest alternating length in this ring.

awsl~

This is my T code in test 14. The reason for T is that I have traversed the starting point. As a matter of fact, it can be finished by sweeping back

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2e5+10;
char s[N];
int ans=0;
int main()
{
    scanf("%s",s);
    int n=strlen(s);
    for(int i=n;i<2*n;++i)
        s[i]=s[i-n];
    for(int i=0;i<n;++i)     //Ergodic starting point 0~n-1
    {
        int tmp=0;
        for(int j=i;j<i+n-1;++j)    //Guaranteed length not greater than n
        {
            if(s[j+1]!=s[j])
                ++tmp;
            else break;
        }
        ans=max(tmp+1,ans);         //tmp records the number that is larger at the back than at the front, plus the starting point
        if(ans==n) break;
    }
    printf("%d\n",ans);
    //cout<<strlen(s)<<endl;
    //cout<<s<<endl;

}

A code

int main()
{
    scanf("%s",s);
    int len=strlen(s);
    for(int i=len;i<2*len;++i)
        s[i]=s[i-len];
    //cout<<s<<endl;
    len<<=1;
    int tmp=1;
    for(int i=1;i<len;++i)
    {
        if(s[i]!=s[i-1])
        {
            if(i==len-1) ans=max(ans,tmp);    //Bw bwbw this situation
            
            tmp++;
            
        }
        else
        {
            ans=max(ans,tmp);
           
            tmp=1;
        }

    }
    len>>=1;
    ans=min(ans,len);
    cout<<ans<<endl;

}

Greedy thinking

Posted by napurist on Wed, 13 Nov 2019 08:04:26 -0800