问题 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;
}