224. 连续字母

题目描述

给你一个字符串,只包含大写字母,求同一字母连续出现的最大次数。例如”AAAABBCDHHH”,同一字母连续出现的最大次数为4,因为一开始A连续出现了4次。

解答要求时间限制:1000ms, 内存限制:100MB
输入

每组输入第一行有一个整数cases(1<=cases<=100),表示有cases个测试数据。
接下来每行有一个子串(1<长度<=100)。

输出

输出对应每个子串同一字母连续出现的最大次数。

样例

输入样例 1 复制

3
AAAABBCDHHH
ISDHFSHFDAASDIAHSD
EEEEEEE

输出样例 1

4
2
7
提示样例 1
思路:从首位开始,按位判断,用count计数,max储存最大的数,如果此位与前一位相同,那么count++,用max比较一下,储存最大值在max里,如果和前一位不相同的话,则count重置为1。
 
代码:
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.

int main()
{
    // please define the C input here. For example: int n; scanf("%d",&n);
    // please finish the function body here. 
    // please define the C output here. For example: printf("%d
",a); 
    char str[105];
    int N;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%s",str);
        char pre = str[0];
        int max = 0;
        int show_time = 1;
        for(int i = 1;i<strlen(str);i++) // 记录连续的字母的数量
        {
            if(str[i] == pre)
            {
                show_time ++;
                if(show_time>max)
                {
                    max = show_time;
                }
              //  printf("%d ",show_time);
            }
            else{
                show_time = 1;    //不相等则重置
                pre = str[i];
                //printf("%c ",pre);
            }
        }
        if(show_time>=max)
        {
            max = show_time;
        }
        printf("%d
",max);
    }

    return 0;
}
以大多数人努力程度之低,根本轮不到去拼天赋~
原文地址:https://www.cnblogs.com/gcter/p/15470479.html