题解 | 穷哈哈~
穷哈哈~
https://www.nowcoder.com/practice/5b3184b233f34fb39a7f259ae82eb42c
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define all(x) (x).begin(),x.end()
#define endl '\n'
void solve_double_p(){
int n;
cin>>n;
string s;
cin>>s;
int ans=0;
int op[2]={1,2};
for(int i=0;i<n;i++){
int k=0,c=0;
if(s[i]=='a')k=1;
else if(s[i]=='h')k=2;
if(k){
c++;
while(k){
i++;
if((s[i]=='a'&&k==2)||(s[i]=='h'&&k==1)){
c++;
k=(k==1?2:1);
}else{
k=0;
}
}
i--;
}
ans=max(ans,c);
}
cout<<ans;
}
void DP(){
int n;
cin>>n;
string s;
cin>>s;
int c=0,ans=0;
char ch='0';
for(int i=0;i<n;i++){
if(s[i]=='a'){
if(ch=='h')c++;
else c=1;
}else if(s[i]=='h'){
if(ch=='a')c++;
else c=1;
}
else c=0;
ans = max(ans,c);
ch=s[i];
}
cout<<ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
DP();
return 0;
}
查看17道真题和解析