2017暑期ACM俱乐部个人训练赛第5场 J题



问题 J: 【枚举】聊斋

时间限制: 1 Sec   内存限制: 64 MB

题目描述

某人读完《聊斋志异》,编出这样一道题。题目为:现有男鬼(b)、女鬼(r)、小鬼(w)共n人(n<400),围站成一环(小鬼可以算是男的也可以算是女的)。从某人开始分别在两边数连续相同性别数(两边的性别可不同),直到第一次发现不同性别为止。当前数的这个人至少要同左右两边中的一方具有相同性别,并算到总数中去。

输入

第1行为人的数目n,第2行为n个字符(只有b,r,w三个情况)。

输出

可找到的连续最大同性别总数。

样例输入

29

wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

样例输出

11



分析:

不知道说这题什么好     按照题意        拟定测试数据为

10

rbrbrbrbrb

输出结果应该为  0   但是就是交不过    后来去掉最后一句话只求一个字母两边最大相同字母个数    就一下过了     不知道是题目问题还是我理解有误    还请指正




AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
char str[1000];
int main (){
    int n;
    //freopen("data","r",stdin);
    while (scanf ("%d",&n)!=EOF){
        scanf ("%s",str);
        if(n==1){
            printf ("1\n");
            continue;
        }
        int maxn=0;
        for (int i=0;i<n;i++){
            char temp=str[i];
            int index=i+1;
            int count=1;
            if(temp!='w'){
                int tt=0;
                while (index!=i){
                    if(str[index]=='w'||str[index]==temp)
                        count++;
                    else{
                        tt++;
                        if(tt>=2)
                            break;
                        else{
                            temp=str[index];
                            count++;
                        }

                    }
                    index=(index+1)%n;
                }

            }
            else{
                int tt=0;
                while (index!=i){
                    if(str[index]=='w')
                        count++;
                    else{
                        temp=str[index];
                        break;
                    }
                    index=(index+1)%n;
                }
                while (index!=i){
                    if(str[index]=='w'||str[index]==temp)
                        count++;
                    else{
                        tt++;
                        if(tt>=2)
                            break;
                        else{
                            temp=str[index];
                            count++;
                        }

                    }
                    index=(index+1)%n;
                }
            }
            if (maxn<count)
                maxn=count;
        }
        printf ("%d\n",maxn);
    }
    return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值