首页 > 其他 > 详细

3624. 三值字符串

时间:2021-06-04 01:32:17      阅读:17      评论:0      收藏:0      [点我收藏+]

双指针模板题。

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(true)
        {
            while(r < s.size() && !check(cnt))
                cnt[s[r++]-‘0‘]++;

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

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

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

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

3624. 三值字符串

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!