首页 > 试题广场 >

穷哈哈~

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

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


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

输入

7
abacaba

输出

1
示例2

输入

20
ahahahahahahahahahah

输出

20
N = int(input())
S = input()

max_len = 0
if S[0]=='a' or S[0]=='h':
    tmp_len = 1
else:
    tmp_len = 0
for i in range(1,len(S)):
    if (S[i]!='a' and S[i]!='h') or S[i-1:i+1]=='aa' or S[i-1:i+1]=='hh':
        if tmp_len>max_len:
            max_len = tmp_len
        tmp_len = 0
    if S[i]=='a' or S[i]=='h':
        tmp_len += 1
        if tmp_len>max_len:
            max_len = tmp_len

print(max_len)
发表于 2025-08-24 15:56:07 回复(0)
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)
#include <stdio.h>
#include <string.h>

int main() {
    int N;
    scanf("%d", &N);
    char str[N+1];
    scanf("%s", str);

    int maxlenth = 0;
    int currentlenth = 0;
    char lastchar = '\0';

    //外层循环遍历字符串每个字符
    for (int i = 0; i < N; i++) {
        //判断字符串中的字母是不是‘a’或者'h',是则进行有效长度计算。
        if (str[i] == 'a' || str[i] == 'h') {
            //如果lastchar是‘\0’表示还没有处理过合法有效字符;
            //s[i] !=lastchar 表示当前字符与上一字符不同,为交替出现字符,是合法有效字符
            if (lastchar == '\0' || str[i] != lastchar) {
                currentlenth ++;
                maxlenth = maxlenth > currentlenth ? maxlenth : currentlenth;
                lastchar = str[i];
            } else {
                currentlenth = 1;
                lastchar = str[i];
            }
        } else {//当前字母不是‘a’或者'h',当前有效字符长度为0,移动到下一个字符处理。
            currentlenth = 0;
            lastchar = '\0';
        }
    }

    printf("%d", maxlenth);
    return 0;
}
发表于 2025-07-09 11:41:46 回复(0)
N=int(input())
S=list(input())
k=0
k1=0
for i in range(N):
    if (S[i-1]=='a'and S[i]=='h') or (S[i-1]=='h'and S[i]=='a'):
        k1+=1
    elif ( S[i-1] =='a' ) or (S[i-1] =='h' ):
        k1=1
    if k1>k:
        k=k1
print(k)
发表于 2025-08-13 16:49:47 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a, maxl = 0, maxll = 0;
    cin >> a;
    string s;
    cin >> s;
    for (int i = 1; i < a; i++) {
        if (s[i - 1] == 'a') {
            if (maxll == 0)//将第一个a计数
                maxll++;
            if (s[i] == 'h') {//符合要求计数
                maxll++;
            } else {//不符合要求,比较记录最大长度并重置此阶段计数
                maxl = max(maxll, maxl);
                maxll = 0;
            }
        }
        if (s[i - 1] == 'h') {
            if (maxll == 0)//将第一个h计数
                maxll++;
            if (s[i] == 'a') {//符合要求计数
                maxll++;
            } else {//不符合要求,比较记录最大长度并重置此阶段计数
                maxl = max(maxll, maxl);
                maxll = 0;
            }

        }
        if (i == a - 1)//循环到字符串结束时比较记录最大长度
            maxl = max(maxll, maxl);
    }
    cout << maxl;
}

发表于 2025-08-08 20:54:06 回复(0)
import sys

n = int(input())
s = input()

def ok(a,b):
    if (a == 'a' or a == 'h') and (b =='a' or b == 'h') and a != b:
        return True
    else:
        return False

if s[0] == 'a' or s[0] == 'h':
    long = 1
else:
    long = 0
lens = 0

for i in range(1,n):
    if (ok(s[i],s[i-1])):
        lens = max(2,lens+1)
    else:
        long = max(long,lens)
        lens = 0
long = max(long,lens)
print(long)
发表于 2025-11-18 13:31:59 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;cin>>n;
    string s,t;
    cin>>s;
    int res=0;
    for(int i=0;i<n;i++){
        if((s[i]=='a'||s[i]=='h')&&t.length()==0){
            t.push_back(s[i]);
            res=max(res,(int)t.length());
        }
        else if(s[i]=='h'){
            if(t[t.length()-1]=='a'){
                t.push_back(s[i]);
                res=max(res,(int)t.length());
            }
            else {
                // res=max(res,(int)t.length());
                t.clear();
                t.push_back(s[i]);
            }
        }
        else if(s[i]=='a'){
            if(t[t.length()-1]=='h'){
                t.push_back(s[i]);
                res=max(res,(int)t.length());
            }
            else{
                // res=max(res,(int)t.length());
                t.clear();
                t.push_back(s[i]);
            }
        }
        else t.clear();
        // res=max(res,(int)t.length());
    }
   
    cout<<res;
}
// 64 位输出请用 printf("%lld")
发表于 2025-10-15 01:54:39 回复(0)
#include <iostream>
#include <string>
using namespace std;


int main() {
    int n;
    cin>>n;
    string s;
    cin>>s;
    int c=0,max=0;
   
    if(s[0]=='a' || s[0]=='h'){
        c=1;
        max=c;
    }

    for (int i=1; i<n; i++) {
        if(s[i-1]=='a' || s[i-1]=='h'){
            if (c==0) {
                c=1;
            }
            if((s[i-1]=='a'&&s[i]=='h') || (s[i-1]=='h'&&s[i]=='a') ){
                c++;
            }else {
                c=0;
            }
            if (c>max) {
                max=c;
            }
        }
        else {
            c=0;
        }
    }

    cout<<max;

}

发表于 2025-08-03 14:58:03 回复(0)
n = int(input())
s = input()
s = s + 'g'
l = []
count = 0
for i in range(len(s)):
    if count == 0:
        if s[i] == 'a'&nbs***bsp;s[i] == 'h':
            count += 1
    else:
        if s[i] == 'a' and s[i-1] == 'h':
            count += 1
        elif s[i] == 'h' and s[i-1] == 'a':
            count += 1
        elif s[i] == 'a'&nbs***bsp;s[i] == 'h':
            l.append(count)
            count = 1
        else:
            l.append(count)
            count = 0
if l == []:
    print(0)
else:
    print(max(l))

发表于 2025-07-31 11:20:07 回复(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)
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int a, b;
    string s;
    cin >> a >> s;
    vector<char> v;
    int max_len = 0;
    int temp_len = 0;
    char tar = 'b';
    for (int i = 0; i < s.size(); i++) {
        if ((tar != 'a' && tar != 'h')) {
            if (s[i] == 'a' || s[i] == 'h') {
                temp_len++;
                tar = s[i] == 'a' ? 'h' : 'a';
            } else {
                max_len = max(max_len, temp_len);
                temp_len = 0;
                continue;
            }
        }
       
        if ((tar == 'a' || tar == 'h')) {
            if (s[i] == tar) {
                tar = tar == 'a' ? 'h' : 'a';
                temp_len++;
            } else {
                max_len = max(max_len, temp_len);
                temp_len = 0;
                if (s[i] == 'a' || s[i] == 'h') {
                    temp_len++;
                    tar = s[i] == 'a' ? 'h' : 'a';
                } else {
                    tar = 'b';
                }
            }

        }
    }
    max_len = max(max_len, temp_len);
    cout << max_len << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2025-06-18 09:06:04 回复(0)
1
发表于 2025-03-18 11:23:52 回复(1)