zoukankan      html  css  js  c++  java
  • Balls of Buma

    Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls).

    When the ball is inserted the following happens repeatedly: if some segment of balls of the same color became longer as a result of a previous action and its length became at least $$$3$$$, then all the balls of this segment are eliminated.

    Consider, for example, a row of balls 'AAABBBWWBB'. Suppose Balph chooses a ball of color 'W' and the place to insert it after the sixth ball, i. e. to the left of the two 'W's. After Balph inserts this ball, the balls of color 'W' are eliminated, since this segment was made longer and has length $$$3$$$ now, so the row becomes 'AAABBBBB'. The balls of color 'B' are eliminated now, because the segment of balls of color 'B' became longer and has length $$$5$$$ now. Thus, the row becomes 'AAA'. However, none of the balls are eliminated now, because there is no elongated segment.

    Help Balph count the number of possible ways to choose a color of a new ball and a place to insert it that leads to the elimination of all the balls.

    Input

    The only line contains a non-empty string of uppercase English letters of length at most $$$3 cdot 10^5$$$. Each letter represents a ball with the corresponding color.

    Output

    Output the number of ways to choose a color and a position of a new ball in order to eliminate all the balls.

    Examples

    Input

    Copy

    BBWWBB
    

    Output

    Copy

    3
    

    Input

    Copy

    BWWB
    

    Output

    Copy

    0
    

    Input

    Copy

    BBWBB
    

    Output

    Copy

    0
    

    Input

    Copy

    OOOWWW

    Output

    Copy

    0
    

    Input

    Copy

    WWWOOOOOOWWW
    

    Output

    Copy

    7
    

    ​ 正确

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+7;
    int num[3*N];
    char al[3*N];
    int main()
    {
        string s;
        cin>>s;
        int len=s.size();
        int l=1;
        int cnt=0;
        for(int i=1;i<len;i++)
        {
            if(s[i]!=s[i-1])
            {
                al[cnt]=s[i-1];
                num[cnt++]=l;
                l=1;
            }
            else
                l++;
        }
        num[cnt]=l;
        al[cnt]=s[len-1];
       // for(int i=0;i<=cnt;i++) cout<<num[i]<<' ';
       if((cnt+1)%2==0) {
            cout<<0<<endl;
       return 0;
       }
       bool flag=0;
       for(int i=0;i<=cnt;i++)
       {
           if(al[i]!=al[cnt-i]||num[i]+num[cnt-i]<3)
           {
               flag=1;
               break;
           }
       }
       if(flag) cout<<0<<endl;
       else cout<<num[cnt/2]+1<<endl;   //而非长度3
       return 0;
    
    }
    
    
  • 相关阅读:
    Knative Serving 进阶: Knative Serving SDK 开发实践
    从求生存到修体系,我在阿里找到了技术人的成长模式
    K8s 学习者绝对不能错过的最全知识图谱(内含 56个知识点链接)
    P1197 [JSOI2008]星球大战
    P1311 选择客栈
    P2822 组合数问题
    贪心 加工生产调度
    P3375 【模板】KMP字符串匹配
    P1025 数的划分
    P1019 单词接龙
  • 原文地址:https://www.cnblogs.com/xxffxx/p/13613132.html
Copyright © 2011-2022 走看看