题解 | 穷哈哈~

穷哈哈~

https://www.nowcoder.com/practice/5b3184b233f34fb39a7f259ae82eb42c

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin>>n;
	string a;
	cin>>a;
	stack<char> e;
	int ans=0;
	for(int i=0;i<n;i++){
		if(e.empty()&&(a[i]=='a'||a[i]=='h')){
			e.push(a[i]);
		}
		else if(!e.empty()){
			if(e.top()=='a'){
				if(a[i]=='h'){
					e.push(a[i]);
				}
				else{
					ans=max(ans,int(e.size()));
					while (!e.empty()){
						e.pop();
					}
					if(a[i]=='a'){
					e.push(a[i]);
					}
				}
				}
				else if(e.top()=='h'){
				if(a[i]=='a'){
					e.push(a[i]);
				}
				else{
										ans=max(ans,int(e.size()));
					while (!e.empty()){
						e.pop();
					}
					if(a[i]=='h'){
					e.push(a[i]);	
					}
				}
			}
	}
	}
						ans=max(ans,int(e.size()));
	cout<<ans;
	return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务