C. Queries for the Array

Monocarp had an array aa consisting of integers. Initially, this array was empty.

Monocarp performed three types of queries to this array:

  • choose an integer and append it to the end of the array. Each time Monocarp performed a query of this type, he wrote out a character +;
  • remove the last element from the array. Each time Monocarp performed a query of this type, he wrote out a character -. Monocarp never performed this query on an empty array;
  • check if the array is sorted in non-descending order, i.,e. a1≤a2≤⋯≤aka1≤a2≤⋯≤ak, where kk is the number of elements in the array currently. Every array with less than 22 elements is considered sorted. If the array was sorted by the time Monocarp was performing that query, he wrote out a character 1. Otherwise, he wrote out a character 0.

You are given a sequence ss of qq characters 0, 1, + and/or -. These are the characters that were written out by Monocarp, given in the exact order he wrote them out.

You have to check if this sequence is consistent, i. e. it was possible for Monocarp to perform the queries so that the sequence of characters he wrote out is exactly ss.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

Each test case consists of one line containing the string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105). This string consists of characters 0, 1, + and/or -. This is the sequence of characters written by Monocarp, in the order he wrote them.

Additional constraints on the input:

  • for every prefix of ss, the number of characters + on it is not less than the number of characters - on it. In other words, if Monocarp actually performed these queries, he would never try to remove the last element from the empty array;
  • the sum of |s||s| over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print YES if it was possible for Monocarp to perform the queries so that the sequence of characters he wrote is exactly ss. Otherwise, print NO.

You can print each letter in any register.

Example

input

Copy

 

7

++1

+++1--0

+0

0

++0-+1-+0

++0+-1+-0

+1-+0

output

Copy

YES
NO
NO
NO
YES
NO
NO

Note

In the first test case, Monocarp could perform the following sequence of queries:

  • add the integer 1313;
  • add the integer 3737;
  • check that the current array [13,37][13,37] is sorted in non-descending order (and it is sorted).

In the fifth test case, Monocarp could perform the following sequence of queries:

  • add the integer 33;
  • add the integer 22;
  • check that the current array [3,2][3,2] is sorted (it is not);
  • remove the last element;
  • add the integer 33;
  • check that the current array [3,3][3,3] is sorted (it is);
  • remove the last element;
  • add the integer −5−5;
  • check that the current array [3,−5][3,−5] is sorted (it is not).

In all other test cases of the example test, it is impossible for Monocarp to write the sequence ss when performing the queries according to the statement.

思路:对于所有的 + 操作,默认放 1,但规定未被固定

           对于所有的0,把前一位修改为0

           对于所有的1,判断前面是否出现过0,若出现过则NO,否则将所有的1固定为不能改变

举例子:如果出现 ++++1--0 这个0操作是不能把前面的1变成0的,因为这个1操作已经固定了之                   前的1,所以答案是NO

要维护这个只需要记录最后一位被固定的一的位置,如果当前要改变的1不是被固定的1就可以改变了

#include<iostream>
#include<cstring>

using namespace std;

const int N = 2e6 + 10;

int ans[N];

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		bool flag = true;
		string s;
		cin >> s;
		int cnt = 0;
		int dis0 = -1;//最前面的0
		int dis1 = -1;//最后一位被固定的1
		for(int i = 0; s[i]; i ++)
		{
			if(s[i] == '+')
				ans[cnt ++] = 1;
			else if(s[i] == '-')
			{
				if(ans[cnt - 1] == 0 && dis0 == cnt - 1)
				dis0 = -1;
				if(ans[cnt - 1] == 1 && dis1 == cnt - 1)//维护最后一位被固定的1
				dis1 --;
				cnt --;	
			}
			else if(s[i] == '1')
			{
				if(cnt < 2)
				continue;
				else 
				{
					dis1 = cnt - 1;
					if(dis0 != -1)//前面出现过0
					{
						flag = false;
						break;
					}
				}
			}
			else if(s[i] == '0')
			{
				if(cnt < 2)
				{
					flag = false;
					break; 
				}
				if(dis0 != -1)
				continue;
				else 
				{
					if(dis1 == cnt - 1)
					{
						flag = false;
						break;
					}
					if(dis0 == -1)
					dis0 = cnt - 1;
					ans[cnt - 1] = 0;
				}
			}
		}
		if(flag)
		cout << "YES" << endl;
		else
		cout << "NO" << endl;
	}
 } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值