每日一题 五十九期 Codeforces Round 859 (Div. 4)

C. Find and Replace

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given a string s s s consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with 0 \texttt{0} 0 or replace all occurrences of this character with 1 \texttt{1} 1.

Is it possible to perform some number of moves so that the resulting string is an alternating binary string † ^{\dagger} ?

For example, consider the string abacaba \texttt{abacaba} abacaba. You can perform the following moves:

  • Replace a \texttt{a} a with 0 \texttt{0} 0. Now the string is 0b 0c 0b 0 \color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}\texttt{c}\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}} 0b0c0b0.
  • Replace b \texttt{b} b with 1 \texttt{1} 1. Now the string is 0 10c0 10 {\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}\texttt{c}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}} 010c010.
  • Replace c \texttt{c} c with 1 \texttt{1} 1. Now the string is 010 1010 {\texttt{0}}{\texttt{1}}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}{\texttt{1}}{\texttt{0}} 0101010. This is an alternating binary string.

† ^{\dagger} An alternating binary string is a string of 0 \texttt{0} 0s and 1 \texttt{1} 1s such that no two adjacent bits are equal. For example, 01010101 \texttt{01010101} 01010101, 101 \texttt{101} 101, 1 \texttt{1} 1 are alternating binary strings, but 0110 \texttt{0110} 0110, 0a0a0 \texttt{0a0a0} 0a0a0, 10100 \texttt{10100} 10100 are not.

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2000 1 \leq n \leq 2000 1n2000) — the length of the string s s s.

The second line of each test case contains a string consisting of n n n lowercase Latin characters — the string s s s.

Output

For each test case, output “YES” (without quotes) if you can make the string into an alternating binary string, and “NO” (without quotes) otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Example
inputCopy
8
7
abacaba
2
aa
1
y
4
bkpt
6
ninfia
6
banana
10
codeforces
8
testcase
outputCopy
YES
NO
YES
YES
NO
YES
NO
NO
Note

The first test case is explained in the statement.

In the second test case, the only possible binary strings you can make are 00 \texttt{00} 00 and 11 \texttt{11} 11, neither of which are alternating.

In the third test case, you can make 1 \texttt{1} 1, which is an alternating binary string.


#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;

int t;
int n, m;
int a[N];
int st[N];
int main()
{
	string s;
	cin >> t;
	while(t --){
		int n, flag = 0;
		memset(st, 0, sizeof st);
		cin >> n;
		cin >> s;
		for(int i = 0; i < s.size(); i ++)
		{
			if(st[i]) continue;
			for(int j = i + 1; j < s.size(); j ++)
			{
				if(s[j] == s[i] && (j - i) % 2 == 1) 
				{
					flag = 1, st[j] = 1;   
                    break;
				}
			}
		}
		if(flag) puts("NO");
		else puts("YES");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值