2019/12/01 CF-1267-B Balls of Buma

2019-2020 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)

题目链接:

 http://codeforces.com/contest/1267/problem/B

测试样例:

input
BBWWBB
output
3

input
BWWB
output
0

input
BBWBB
output
0

input
OOOWWW
output
0

input
WWWOOOOOOWWW
output
7

思路:

       将连续相同的合并为一个集合,记录字母及长度
       集合个数是偶数一定不能消除
       集合个数是奇数则要满足是回文串且中间集合长度大于1且两边各集合长度至少一个大于等于2

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+1;
string s;
struct node
{
    int len;//长度
    char ch;//字母
}a[maxn];
int main()
{
    cin>>s;
    int l=s.size();
    int cnt=0,index=0;//cnt计数连续相同字母出现了多少次,index为组合个数
    char c=s[0];
    for(int i=0;i<l;i++)
    {
        if(c!=s[i])
        {
            a[index].len=cnt;
            a[index++].ch=c;
            cnt=0;//重置cnt
            c=s[i];//更新c
        }
        cnt++;
    }
    a[index].len=cnt;//最后一组
    a[index].ch=c;
    int flag=1;
    //组合数为偶数或者中间组合长度小于2(注意index下标从0开始)
    if(index&1!=0||a[index/2].len==1)
    {
        printf("0\n");
        return 0;
    }
    for(int i=index/2-1;i>=0;i--)//从中间向两边扫描
    {
        //不满足回文串或者长度相加也不会大于等于3
        if(a[i].ch!=a[index-i].ch||a[i].len<2&&a[index-i].len<2)
        {
            flag=0;
            break;
        }
    }
    if(flag)
        printf("%d\n",a[index/2].len+1);
    else
        printf("0\n");
    return 0;
}

        

发布了26 篇原创文章 · 获赞 1 · 访问量 444

猜你喜欢

转载自blog.csdn.net/qq_45309822/article/details/103337452
今日推荐