首页 > 试题广场 >

穷哈哈~

[编程题]穷哈哈~
  • 热度指数:10891 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
"你叉叉,唱日出,穷哈哈,唱日落.....",小哈开心地哼着小调,因此小哈是一个爱笑的人,每次笑都很有魔性,调皮地小哼记录了小哈的一次说的话,其中里面可能包含了小哈的笑声,并以为字符串来记录小哈的话。已知,小哈的笑声是字母交替的序列,例如:,,是符合笑声的合法序列。但是,,不符合笑声的合法序列。
通过小哼的记录,请你求出小哈笑声的最大长度。

输入描述:
输入的第一行给出小哈说话的长度
随后一行中输入一行长度为字符串——表示小哈的话。
{1 \leq N \leq 10^5}
仅由小写字母组成。


输出描述:
输出小哈笑声的最大长度。
示例1

输入

7
abacaba

输出

1
示例2

输入

20
ahahahahahahahahahah

输出

20
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String s = in.next();
        in.close();

        int maxLen = 0;
        int currLen = 0;
        char last = 0; // ASCII中0不是任何可见字符,相当于null

        for (int i = 0; i < n; i++) { 
            char c = s.charAt(i);

            // 只处理a和h,其他字符直接重置
            if (c != 'a' && c != 'h') {
                currLen = 0;
                last = 0;
                continue;
            }

            // 当前是合法字符时的处理
            if (last == 0) {
                // 首次遇到合法字符,初始化序列
                currLen = 1;
            } else if (c != last) {
                // 与上一个字符不同,延长序列
                currLen++;
            } else {
                // 与上一个字符相同,重置为当前字符的新序列
                currLen = 1;
            }

            // 更新上一个字符和最大长度,实时更新,这里很重要!
            last = c;
            if (currLen > maxLen) {
                maxLen = currLen;
            }
        }

        System.out.println(maxLen);
    }
}

发表于 2025-08-26 20:44:17 回复(0)
可以试着分开几个场景去判断:没有a和h、只有a、只有h、有a和h。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int N = in.nextInt();
            in.nextLine();
            String str = in.nextLine();
            ArrayList<Integer> list = new ArrayList<>();
            int numA = 0, numH = 0;

            // 统计'a'和'h'的数量
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c == 'a') numA++;
                else if (c == 'h') numH++;
            }

            if(numA == 0 && numH == 0){
                System.out.println("0");
                continue;
            }

            // 处理只有一种字符的情况
            if ((numA == 0 && numH != 0) || (numH == 0 && numA != 0)) {
                System.out.println("1");
                continue;
            }

            // 统计交替序列的最大长度
            int currentLength = 1;
            for (int i = 1; i < str.length(); i++) {
                char prev = str.charAt(i - 1);
                char curr = str.charAt(i);
                if ((prev == 'a' && curr == 'h') || (prev == 'h' && curr == 'a')) {
                    currentLength++;
                } else {
                    list.add(currentLength);
                    currentLength = 1;
                }
            }
            list.add(currentLength); // 添加最后一个序列的长度

            // 输出最大长度
            if (list.isEmpty()) {
                System.out.println("0");
            } else {
                int max = list.get(0);
                for (int len : list) {
                    if (len > max) max = len;
                }
                System.out.println(max);
            }
        }
        in.close();
    }
}



发表于 2025-07-18 15:04:24 回复(0)