3624. 三值字符串

双指针模板题。

string s;

bool check(int cnt[])
{
    for(int i=1;i<=3;i++)
        if(cnt[i] == 0)
            return false;
    return true;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>s;

        int l=0,r=0;
        int cnt[4]={0};

        int ans=s.size()+1;
        while(l < s.size())
        {
            while(r < s.size() && !check(cnt))
                cnt[s[r++]-'0']++;

            if(check(cnt)) ans=min(ans,r-l);

            cnt[s[l++]-'0']--;
        }

        if(ans > s.size()) ans=0;
        cout<<ans<<endl;
    }

    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/fxh0707/p/14847713.html