首页 > 试题广场 >

穷哈哈~

[编程题]穷哈哈~
  • 热度指数:18386 时间限制: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)
#include <iostream>
using namespace std;
#include <string>

int main(){
    int N;
    string s;
    cin >> N >> s;
    int lenth = 0;
    int lenth_max = 0;
    char prv = 0;
    for(const auto& c : s){
        if(c != 'a' && c != 'h'){
            lenth = 0; 
            prv = 0;
            continue;
        }
        if(prv = 0 || c != prv){
            lenth++;
            prv = c;
            if(lenth > lenth_max) lenth_max = lenth;
        }
        else{
            lenth = 1;
            prv = c;
        }
    }
    
    cout << lenth_max << endl;
    return 0;
}

发表于 2026-02-03 02:28:16 回复(0)
import sys

n, s, c, m = int(sys.stdin.readline()), sys.stdin.readline(), 0, 0
for i in range(n):
    if s[i] in ("a", "h"):
        if i > 0 and s[i] != s[i - 1] and s[i - 1] in ("a", "h"):
            c += 1
        else:
            c = 1
    else:
        c = 0
    m = max(c, m)
print(m)

发表于 2026-01-19 01:21:07 回复(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 java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String S = in.next();
        int res = 0;//结果信息
        int index = 0;//遍历标志
        while (index < N) {
            //判断笑声的起始字符是a或b开头
            if (S.charAt(index) == 'a') {//起始字符为a进入判断
                int h = 1;//这里用h的奇偶性来回判断a、h 还能存储合法字符长度
                while (index + 1 < N) {//判断后续字符是否是h、a之间变化
                    if(h%2==1 && S.charAt(index+1)=='h') {
                        index++;
                        h++;
                    }
                    else if(h%2==0 && S.charAt(index+1)=='a'){
                        index++;
                        h++;
                    }else break;//都不是跳出字符串判断
                }
                if(h>res) res=h;//存储最大合法字符长度
            //下方代码只要调换上方判断语句的a和h位置就行
            } else if (S.charAt(index) == 'h') {
                int h = 1;
                while (index + 1 < N) {
                    if(h%2==1 && S.charAt(index+1)=='a') {
                        index++;
                        h++;
                    }
                    else if(h%2==0 && S.charAt(index+1)=='h'){
                        index++;
                        h++;
                    }else break;
                }
                if(h>res) res=h;
            }
            index++;
        }
        System.out.println(res);
    }
}
发表于 2026-04-10 23:21:02 回复(1)
n = int(input())
s = input()

max_len = 0
cur_len = 0
prev = ""

for ch in s:
    if ch in ("a", "h"):
        if prev in ("a", "h") and ch == prev:
            cur_len = 1
        else:
            cur_len += 1
        prev = ch
    else:
        cur_len = 0
        prev = ""

    if cur_len > max_len:
        max_len = cur_len

print(max_len)

发表于 2026-03-08 20:10:28 回复(0)
int main() {
    int n = 0;
    int count = 0;
    char arr[1000000];
    int i = 0;
    scanf("%d", &n);
    getchar();//清空' '空格字符

//数组赋值
    for (i = 0; i < n; i++) {
        scanf("%c", &arr[i]);
    }
    arr[i] = '\0';

//统计
    int maxcount = 0;
    for (int j = 0; j < n; j++) {

        if ((arr[j] == 'a' && arr[j + 1] == 'h') || (arr[j] == 'h' &&
                arr[j + 1] == 'a')) {
            count++;
            if (count > maxcount) {
                maxcount = count;
            }
        }

        else {
            count = 0;
        }
    }

if (maxcount == 0) {         //注意这一块,容易遗漏。
for (int x = 0; x < n; x++) { //单个字符不是'a'或'h'的情况,或者"abxjaiw"只有只有单有效字符的情况
            if (arr[x] == 'a' || arr[x] == 'h') {
                maxcount = 1;
                break;
            }
        }
        printf("%d", maxcount);
        return 0;
    }

    printf("%d", maxcount + 1); //+1解释 "ahcbdes"
    return 0;
发表于 2026-02-28 22:33:31 回复(0)
#include <iostream>
using namespace std;

int main() {
    int N,m=0,l=0,sgl=-1;cin>>N;
    string s;cin>>s;
    for(int i=0;i<N;i++){
        if(s[i]=='a'&&sgl==-1){
            sgl=0;l++;
        }else if(s[i]=='h'&&sgl==-1){
            sgl=1;l++;
        }else if(s[i]=='a'&&sgl==1){
            sgl=0;l++;
        }else if(s[i]=='h'&&sgl==0){
            sgl=1;l++;
        }else{
            if(m<l) m=l;
            if(s[i]=='a'&&sgl==0){
                sgl=0;l=1;
            }else if(s[i]=='h'&&sgl==1){
                sgl=1;l=1;
            }else l=0;
        }
    }
    if(m<l) m=l;
    cout<<m;
}

发表于 2026-02-20 10:00:44 回复(0)
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
using ull=unsigned long long;
using i128=__int128_t;
using u128=__uint128_t;
using ld=long double;

void solve()
{
    int n,a=0,h=0,MAX=0;
    cin >> n;
    string s;
    cin >> s;
    for(int i=0;i<s.size();i++)//看字符串中是否有a或h 从而判断MAX的初始值
    {
        if(s[i]=='a'||s[i]=='h')
        {
            MAX=1;
            break;
        }
    }
    for(int i=0,j=0;i<s.size()&&j<s.size()-1;) //双指针扫一遍 判断是不是ah交替出现
    {
        if((s[j]=='a'&&s[j+1]=='h')||(s[j]=='h'&&s[j+1]=='a'))
        {
            MAX=max(MAX,j-i+2);
            j++;
        }
        else
        {
            j=i=j+1;
        }
        //cout << MAX <<"\n";
    }
    cout << MAX;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
   
    int t=1;
    //cin >> t;
   
    while(t--)
    {
        solve();
    }
    return 0;
}

发表于 2026-02-10 17:42:32 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;cin>>n;
    string s;cin>>s;
    int len=0;
    int currlen=0;
    for(int i=0;i<n;i++){
if(s[i]=='a'||s[i]=='h'){
    if(!i||s[i]!=s[i-1])currlen++;
    else currlen=1;
}else currlen=0;
len=max(len,currlen);
    }
    cout<<len<<endl;
    return 0;
}
发表于 2026-02-04 14:21:15 回复(0)
#include <stdio.h>
#include<string.h>
int main(){
   int N;
   scanf("%d",&N);
   getchar();
   char s[100001];
   fgets(s,sizeof(s),stdin);
   s[strcspn(s,"\n")]='\0';
   int left=0,max=1,k;
   int n;
    for(n=0;n<N;n++){
        if(s[n]=='a'||s[n]=='h')
        {
             break;
        }
    }
    if(n==N)
    {
      k=0;
      printf("%d",k);
      return 0;
    }
    else
    {
      k=1;
      while(left<N-1)
      {  
          while(left<N-1&&(s[left]=='a'&&s[left+1]=='h'||s[left]=='h'&&s[left+1]=='a'))
          {
              k+=1;
              left+=1;
          }
          if(k>max)
          {
            max=k;
          }
          k=1;
          left+=1;
      }
    }
    printf("%d",max);
}
发表于 2026-02-01 11:52:27 回复(0)
#include <iostream>
using namespace std;
#include<string>
#include<algorithm>
int main() {
    int N;
    cin>>N;
    int cur_len=0,max_len=0;
    string s;
    cin>>s;// 遍历 判断当前字符与前一个字符 判断是否延续了笑声 
    for(int i=0;i<s.size();i++){
        if(s[i]=='a'){
           if(i==0||cur_len==0){
            cur_len++;
           }
        if(i!=0&&s[i-1]=='h'){
            cur_len++;
           }
        if(i!=0&&s[i-1]!='h'){
            max_len=max(cur_len,max_len);
            cur_len=0;
        }   
        }
         if(s[i]=='h'){
          if(i==0||cur_len==0){
            cur_len++;
           }
        if(i!=0&&s[i-1]=='a'){
            cur_len++;
           }
        if(i!=0&&s[i-1]!='a'){
            max_len=max(cur_len,max_len);
            cur_len=0;
        }   
        } 
    }
    max_len=max(cur_len,max_len);
    cout<<max_len;
}
// 64 位输出请用 printf("%lld")

发表于 2026-01-13 15:43:30 回复(0)
这是递归吗
#include <stdio.h>

int la(int* sum,int*max);
int lh(int* sum,int*max){
    char str;
    if(scanf("%c",&str)!=EOF&&str=='a'){
        *sum+=1;
        la(sum,max);
    }
    else{
        //处理连续h的情况,防止因为偶数个h连续,导致ah计数丢失
        if(str=='h'){
            //在重置计数前,先保存目前的最大长度,防止丢失
            if(*sum>*max){
                *max=*sum;
            }
            *sum=1;
            lh(sum,max);
        }
        if(*sum>*max){
            *max=*sum;
        }
        *sum=0;
    }
    return 0;
}
int main() {
    char s;
    int i=0,j=0,k;
    int* sum=&i;
    int*max=&j;
    scanf("%d",&k);
    for(;scanf("%c",&s)!=EOF;){
        if(s=='a'){
            i+=1;
            la(sum,max);
        }
        if(s=='h'){
            i+=1;
            lh(sum,max);
        }
    }
    printf("%d",j);
    return 0;
}

int la(int*sum,int*max){
    char str;
    if(scanf("%c",&str)!=EOF&&str=='h'){
        *sum+=1;
        lh(sum,max);
    }
    else{
        if(str=='a'){
            if(*sum>*max){
                *max=*sum;
            }
            *sum=1;
            la(sum,max);
           
        }
        if(*sum>*max){
            *max=*sum;
        }
        *sum=0;
    }
    return 0;
}

发表于 2026-01-09 23:49:58 回复(0)
#include <stdio.h>
#include <stdlib.h>
void clear(void);
int count_max(char*);
int main() {
    int len;//获得长度
    scanf("%d",&len);
    char *str=calloc(len+1,sizeof(char));//内存分配
    if(!str)
        exit(EXIT_FAILURE);
    clear();//清理缓存区
    scanf("%s",str);
    printf("%d\n",count_max(str));
    free(str);//避免内存泄漏
    return 0;
}
void clear(void)
{
    int ch;
    while((ch=getchar())!='\n'&&ch!=EOF);
}
int count_max(char*str)
{
    int max=0;//用一个变量统计最终的最大值
    int flag;//使用flag记录当前状态(为一则上次录入a,为零则上次录入h)
    int i=0;
    int current_max=0;//使用中间变量随时统计长度
    for(;str[i]!='\0';i++){//利用一个循环确定初始flag
        if(str[i]=='a'){
            flag=1;
            current_max++;
        }
        else if(str[i]=='h'){
            flag=0;
            current_max++;
        }
        else;
        if(current_max>max){//初始flag已确立,移动下标后退出循环
            max=current_max;
            i++;
            break;
        }
    }
    for(;str[i]!='\0';i++){//在上个循环基础上遍历数组
        if(str[i]=='a'&&flag!=1){
            current_max++;
            flag=1;//状态转换
        }
        else if(str[i]=='h'&&flag!=0){
            current_max++;
            flag=0;//状态转换
        }
        else if(str[i]=='a'||str[i]=='h')
                current_max=1;//持续出现aa或hh
        else {
            current_max=0;//出现其他字符
        }
        if(current_max>max)
            max=current_max;//比较更新max
    }
    return max;//返回最终值
}

发表于 2026-01-06 21:29:52 回复(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)