2023杭电多校第8场E题-0 vs 1

题目链接:http://csoj.scnu.edu.cn/contest/102/problem/1005

解题思路:

 

代码如下:

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;

int s[N], l, r;
int now;

int findl(int now)
{
	for (int i = l; i <= r; i++)
	{
		if (s[i] != now) return i; 
		now ^= 1;
	}
	return -1;
}

int findr(int now)
{
	for (int i = r; i >= l; i--)
	{
		if (s[i] != now) return i;
		now ^= 1;
	}
	return -1;
}
int main()
{
	ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	int T;
	cin >> T;
	
	while(T--)
	{
		int n;
		cin >> n;
		string t;
		cin >> t;
		
		for (int i = 1; i <= n; i++)
			s[i] = t[i - 1] & 1;
			
		//找到谁第一次拿到主动权
		l = 1, r = n;
		now = 0;
		while(s[l] != s[r] && l < r)
		{
			if (s[l] == now) l++;
			else if (s[r] == now) r--;
			now ^= 1;
		}
		if (l == r && s[l] == now)
		{
			cout << "-1" << endl;
			continue;
		}
		if (s[l] != now)
		{
			now ^= 1;
			cout << now << endl;
			continue;
		}
		
		int lpos = findl(now); //从左边找到出现连续两个的最近位置 
		int rpos = findr(now);
		
		if ( (lpos != -1 && s[lpos] == now) || (rpos != -1 && s[rpos] == now) )
			cout << now << endl;
		else if (lpos - 1 == rpos || lpos == -1) //特判只有一个!x!x的情况 
			cout << "-1" << endl;
		else cout << s[lpos] << endl;//没有xx序列 且有多个!x!x 
	}
	
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值