zoukankan      html  css  js  c++  java
  • CF1025C Plasticine zebra

    题目描述:

    Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras.

    Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.

    Before assembling the zebra Grisha can make the following operation 00 or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order "bwbbw" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain "wbwbb".

    Determine the maximum possible length of the zebra that Grisha can produce.

    题目大意:

    给你一个长度为 left|s ight|s∣ 的01串 ss,每次操作你可以任选一个 kk ,使01串的 [1,k][1,k] 和 (k,left|s ight|](k,s] 分别翻转(其中一个区间可以为空),求经过任意次操作后能得到的最长的01交替出现的子串的长度。(实际题目中01用w和b代替)

    思路:

    有一个特别神奇的点,我们做一次操作,把分界线两端的01串分别翻转,考虑到如果你把整个串翻转对答案没有影响,把整个串翻转我们就会发现,其实相当于把后一个串接到前一个串之前,于是我们只要把01串围成一个环,计算最长的01交替出现的字串长度,即为答案。

    以下代码:

    #include<bits/stdc++.h>
    #define il inline
    #define _(d) while(d(isdigit(ch=getchar())))
    using namespace std;
    const int N=1e5+5;
    char s[N];int ans,n;
    il int read(){
        int x,f=1;char ch;
        _(!)ch=='-'?f=-1:f;x=ch^48;
        _()x=(x<<1)+(x<<3)+(ch^48);
        return f*x;
    }
    int main()
    {
        scanf( "%s",s+1);
        n=strlen(s+1);int res=1;
        if(n==1){puts("1");return 0;}
        for(int i=2;i<=n;i++){
            if(s[i]==s[i-1]){
                ans=max(ans,res);res=0;
            }
            res++;
        }
        ans=max(ans,res);
        if(s[1]!=s[n]){
            res=1;
            for(int i=2;i<=n;i++){
                if(s[i]==s[i-1])break;
                res++;
            }
            res++;
            for(int i=n-1;i;i--){
                if(s[i]==s[i+1]||res>=n)break;
                res++;
            }
            ans=max(ans,res);
        }
        printf("%d
    ",min(ans,n));
        return 0;
    }
    View Code
  • 相关阅读:
    Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
    关于求解不定方程的n(n-1)=2m(m-1)的解法的总结
    objective-c @()
    objective-c 条件运算符
    关于判断两个矩阵相交的一点想法
    二维几何常用运算
    《为ipad而设计 打造畅销APP》读书笔记
    ios cocos2d FPS过低的解决方法
    python 根据对象和方法名,返回提供这个方法的定义的类
    python 获取类的属性
  • 原文地址:https://www.cnblogs.com/Jessie-/p/10362381.html
Copyright © 2011-2022 走看看