题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1252
1 VS 1
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 234(113 users) Total Accepted: 115(109 users) Rating: Special Judge: No
Description
Alice and Bob are playing the game SanguoSha 1VS1.If Alice take a card or use a card (it may be slash,missed,peach,duel,sabotage or theft and so on) or discard (sometimes he does not need to throw any card) we will write down an uppercase ‘A’, if Bob does this, of course, we will write down the letter “B’. Tell me the length of the longest operation combo performed by either player.
Input
There are several test cases, each test case contains only a string composed of uppercase ‘A’ and ‘B’.The input will finish with the end of file. The length of the string is no more than 1000.
Output
For each the case, output an integer indicate for the length.
Sample Input
AAABBAAAAA
AABBBBAA
AAAAAAAA
Sample Output
5
4
8
Source
Hunan University 2011 the 7th Programming Contest
Recommend
万祥
【思路分析】直接从第二个字符开始与前一个字符比较,然后更新最大值就可以了。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[1005];
int main()
{
while(~scanf("%s",str))
{
int maxn=1,tmp=1;
int len=strlen(str);
for(int i=1;i<len;i++)
{
if(str[i]==str[i-1])
{
tmp++;
maxn=max(tmp,maxn);
}
else
{
maxn=max(maxn,tmp);
tmp=1;
}
}
printf("%d\n",maxn);
}
return 0;
}