【思维】codeforces 1025C Plasticine zebra

版权声明:转载标注来源喔~ https://blog.csdn.net/iroy33/article/details/90048875

codeforces 1025C

题意:给一个只包含w和b的字符串,可以进行裁剪翻转操作 bw|bbw->wbwbb,该操作不限次数,问最大能生成的wb交叉子序列为多少?

又是毫无思路的一题~~!参考博客

该题是酱紫的 bw(头)|(尾)bbw ,生成wb|wbb,把后面的一段移到前面去就是wbbwb ,然后这个就是bwbbw(原串)倒过来念。如果我们把这个字符串看成首尾相连的一个环,那么,不管怎么操作,这些字母的相对位置都是不会改变的(重要特征),也就是说,我们直接统计这个环里面的最长交替出现的长度即可。

awsl~

这是我T在test 14的代码,T的原因是我这遍历了起点。事实上从前往后扫一遍就可以结束

#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)     //遍历起点 0~n-1
    {
        int tmp=0;
        for(int j=i;j<i+n-1;++j)    //保证长度不大于n
        {
            if(s[j+1]!=s[j])
                ++tmp;
            else break;
        }
        ans=max(tmp+1,ans);         //tmp记录的是后面比前面大的个数,还要加上起点
        if(ans==n) break;
    }
    printf("%d\n",ans);
    //cout<<strlen(s)<<endl;
    //cout<<s<<endl;

}

A的代码

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);    //bwbw这种情况
            
            tmp++;
            
        }
        else
        {
            ans=max(ans,tmp);
           
            tmp=1;
        }

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

}

贪心思路

猜你喜欢

转载自blog.csdn.net/iroy33/article/details/90048875
今日推荐