B. A Perfectly Balanced String?

题目链接

B. A Perfectly Balanced String?

给出一个字符串 s ,对于 s 中出现过的任意两个字母 u,v ,定义一个 Balanced 的字符串满足在任何 一个子串中 uv 的出现次数相差不超过 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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16214240.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(70)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示