Codeforces 1082B. Vova and Trophies

探讨了给定仅含G和S的字符串,通过移动字符求最长连续G的算法。解析不同情况,分享AC代码实现,适用于算法竞赛及编程挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当时也是wa的怀疑人生…
给一个长度为n的仅有G和S的字符串,只能移动两个字符一次,求最长的连续G的长度。
分几种情况分别讨论一下就AC啦

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,t;
vector<int> ans;
char s[202020];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	while(cin>>n){
		cin>>s;t = 0;ans.clear();
		for(int i = 0;i<n;){
			if(s[i] == 'G' ){
				ans.push_back(i);
				while(s[i] == 'G'&&i<n) i++;
				ans.push_back(i-1);
			}
			while(s[i] == 'S' && i<n) i++; 
		}
		if(!((int)ans.size())) cout<<0<<endl;
		else if((int)ans.size() == 2) cout<<ans[1] - ans[0] +1<<endl;
		else if((int)ans.size() == 4) {
			if(ans[2]-ans[1] !=2)cout<<max(ans[1]-ans[0]+1,ans[3]-ans[2]+1)+1<<endl;
			else cout<<ans[3]-ans[0]<<endl;
		}
		else {
			int cnt = (int)ans.size()/2;
			for(int i = 1;i<cnt;i++){
				if(ans[i*2] - ans[i*2-1] == 2) t = max(t,ans[i*2+1] - ans[i*2-2]+1);
				else t = max(max(t,ans[2*i-1]-ans[i*2-2]+2),ans[i*2+1]-ans[i*2]+2);
			}
			cout<<t<<endl;
		}
		//for(int i = 0;i<ans.size();i++)	cout<<ans[i]<<' ';cout<<endl; 
	}
	return 0; 
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值