哈理工OJ 1252 1 VS 1(求最长连续的相同的字母的长度)

题目链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值