B. A Perfectly Balanced String?

题目链接

B. A Perfectly Balanced String?

给出一个字符串 \(s\) ,对于 \(s\) 中出现过的任意两个字母 \(u, v\) ,定义一个 Balanced 的字符串满足在任何 一个子串中 \(u\)\(v\) 的出现次数相差不超过 1 . 判断字符串是否满足 Balanced 条件.

解题思路

思维

满足条件的字符串一定是带有循环节的
证明:扫描字符串,直到出现跟前面一样的字符,考虑后面的一个字符,如果这个字符不是两个相等字符中间的任意一个字符,则可取相等字符与该字符使其不满足要求,故该字符一定是位于相等字符中间的字符,如果该字符不是前面一个相等字符的后面一个字符,则可取当前字符与前面一个相等字符的后面一个字符使其不满足要求,得证

  • 时间复杂度:\(O(n)\)

模拟

题目等价于对于每一个字母,判断是否存在这样一个子串:子串不含该字母,且子串中存在出现两次及以上的字母。对于每个字母直接扫描一遍即可

  • 时间复杂度:\(O(26n)\)

  • 思维

// Problem: B. A Perfectly Balanced String?
// Contest: Codeforces - Codeforces Round #785 (Div. 2)
// URL: https://codeforces.com/contest/1673/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
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 s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int t,n;
char s[N];
bool v[26];
int main()
{
	scanf("%d",&t);
    while(t--)
    {
    	scanf("%s",s+1);
    	n=strlen(s+1);
    	memset(v,0,sizeof v);
    	int k=1;
    	for(;k<=n;k++)
    		if(!v[s[k]-'a'])v[s[k]-'a']=true;
    		else
    			break;
    	bool f=true;
    	for(int i=k;i<=n;i++)
    		if(s[i]!=s[i-k+1])
    		{
    			f=false;
    			break;
    		}
    	puts(f?"YES":"NO");
    }
    return 0;
}
  • 模拟
// Problem: B. A Perfectly Balanced String?
// Contest: Codeforces - Codeforces Round #785 (Div. 2)
// URL: https://codeforces.com/contest/1673/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
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 s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int t,n,cnt[26];
bool v[26];
char s[N];
bool ck(int x)
{
	memset(cnt,0,sizeof cnt);
	for(int i=1;i<=n;i++)
	{
		if(s[i]-'a'==x)
			memset(cnt,0,sizeof cnt);
		cnt[s[i]-'a']++;
		if(cnt[s[i]-'a']>=2)return true;
	}
	return false;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
    	memset(v,0,sizeof v);
    	scanf("%s",s+1);
    	n=strlen(s+1);
    	for(int i=1;i<=n;i++)v[s[i]-'a']=true;
    	bool f=true;
    	for(int i=0;i<26;i++)
    		if(v[i])
    		{
    			if(ck(i))
    			{
    				f=false;
    				break;
    			}
    		}
    	puts(f?"YES":"NO");
    		
    }
    return 0;
}
posted @ 2022-05-01 21:53  zyy2001  阅读(63)  评论(0编辑  收藏  举报