Codeforces 1354B

题目在这里

题意: 一个字符串 s s s只有1,2,3,求最小的子串三者都包含.
0 ≤ ∣ s ∣ ≤ 2 e 5 0\leq|s|\leq2e5 0s2e5

分析:
通过归纳,最终的答案形式值只能是 a b b b . . . b b b c abbb...bbbc abbb...bbbc这种形式.感性的想如果现在是 a b . . . b a ab...ba ab...ba,那我前面的a完全可以不要,b只要1个然后去找 c c c.
思路有了,写法就很多样

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

string s;
void solve(){
    cin>>s;
    int n = s.size();
    s = ' '+s;
    vector<pair<int,int>> c;
    for(int i = 1;i<=n;++i){
        if(c.empty()||c.back().first!=s[i]-'0') c.push_back({s[i]-'0',1});
        else c.back().second++;
    }
    int ans  = 0x3f3f3f3f;
    for(int i = 1;i<c.size()-1;++i){
        if(c[i-1].first!=c[i+1].first){
            ans = min(ans,c[i].second+2);
        }
    }
    cout<<(ans==0x3f3f3f3f?0:ans)<<"\n";
}

signed main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

相关推荐

  1. Codeforces 1354B

    2024-06-12 20:16:03       31 阅读
  2. codeforces 1904B

    2024-06-12 20:16:03       58 阅读
  3. codeforces 118 div2(a,b,c)

    2024-06-12 20:16:03       59 阅读
  4. Codeforces Round 932 (Div. 2)(A,B,C,D)

    2024-06-12 20:16:03       33 阅读
  5. codeforces round 936 div2(a,b,c)

    2024-06-12 20:16:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 20:16:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 20:16:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 20:16:03       82 阅读
  4. Python语言-面向对象

    2024-06-12 20:16:03       91 阅读

热门阅读

  1. html 使用input file上传图片并显示

    2024-06-12 20:16:03       32 阅读
  2. 软件许可证管理?

    2024-06-12 20:16:03       27 阅读
  3. 如何有效限制IP多次重新访问网站

    2024-06-12 20:16:03       31 阅读
  4. MySQL 运算符绕过

    2024-06-12 20:16:03       25 阅读
  5. 【react】useState 使用指南

    2024-06-12 20:16:03       31 阅读
  6. linux常用的基础命令

    2024-06-12 20:16:03       24 阅读