codeforces B. Ternary String

在这里插入图片描述

题目

题意:

给你一个字符串 s s s,问里面字串出现 1 , 2 , 3 1,2,3 1,2,3的最短序列是多少。

思路:

我们对于每次 1 , 2 , 3 1,2,3 1,2,3的时候,都去最晚出现的时候,然后我们在这些中每次三个中最大距离最小,这个就是答案了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(ll x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 2e5 + 10;
const int inf = 1e9;
char s[maxn];
int main() {
    int t;
    read(t);
    while (t--) {
        int cnt1 = inf, cnt2 = inf, cnt3 = inf;
        scanf("%s", s);
        int len = strlen(s), Min = inf;
        for (int i = 0; i < len; i++) {
            if (s[i] == '1') cnt1 = i;
            if (s[i] == '2') cnt2 = i;
            if (s[i] == '3') cnt3 = i;
            Min = min(max(cnt1, max(cnt2, cnt3)) - min(cnt1, min(cnt2, cnt3)) + 1, Min);
        }
        if (cnt1 != inf && cnt2 != inf && cnt3 != inf) out(Min);
        else out(0);
        putchar('\n');
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值