0
点赞
收藏
分享

微信扫一扫

Codeforces Round #785 (Div. 2)补题

南陵王梁枫 2022-05-01 阅读 56
算法

目录:

官网链接

B:A Perfectly Balanced String?

Let’s call a string s perfectly balanced if for all possible triplets (t,u,v) such that t is a non-empty substring of s and u and v are characters present in s, the difference between the frequencies of u and v in t is not more than 1.

For example, the strings “aba” and “abc” are perfectly balanced but “abb” is not because for the triplet (“bb”,‘a’,‘b’), the condition is not satisfied.

You are given a string s consisting of lowercase English letters only. Your task is to determine whether s is perfectly balanced or not.

A string b is called a substring of another string a if b can be obtained by deleting some characters (possibly 0) from the start and some characters (possibly 0) from the end of a.
Input
The first line of input contains a single integer t (1 ≤ t ≤ 2⋅104) denoting the number of testcases.

Each of the next t lines contain a single string s (1 ≤ |s| ≤ 2⋅105), consisting of lowercase English letters.

It is guaranteed that the sum of |s| over all testcases does not exceed 2⋅105.

Output
For each test case, print “YES” if s is a perfectly balanced string, and “NO” otherwise.

You may print each letter in any case (for example, “YES”, “Yes”, “yes”, “yEs” will all be recognized as positive answer).
Example
input
5
aba
abb
abc
aaaaa
abcba
output
YES
NO
YES
YES
NO

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define endl '\n'
#define pb push_back

template <typename T> bool chkMax(T &x,T y){return y > x ? x = y,1 : 0;}
template <typename T> bool chkMin(T &x,T y){return y < x ? x = y,1 : 0;}

template <typename T> void inline read(T &x){
	int f = 1;x = 0;char c = getchar();
	while(c < '0' || c > '9'){if(c == '-')	f = -1;c = getchar();}
	while(c >= '0' && c <= '9')	x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
	x *= f;
}

void solve()
{
	string s;
	cin >> s;

	set<char> S;
	for(auto c : s)
		S.insert(c);

	bool flag = true;
	for(int i = 0;i < s.size();i++)
		if(s[i] != s[i % S.size()])
		{
			flag = false;
			break;
		}
		
	if(flag)	cout << "YES" << endl;
	else	cout << "NO" << endl;
	return;
}

int main()
{
	IOS;
	int T;
	cin >> T;
	while(T--)
		solve();
	return 0;
}
举报

相关推荐

0 条评论