Educational Codeforces Round 87 (Rated for Div. 2) 1354B - Ternary String Solution

 


 Solution in C++:

/// La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
/// Abul Hasnat  Tonmoy
/// Department of CSE,23rd batch
/// Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///

#include <bits/stdc++.h>

using namespace std;

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int t,i,j;

    cin>>t;

    while(t--)

    {

        string s;

        int a=-1,b=-1,c=-1,ans=1e9,mx,mn;

        cin>>s;

        for(i=0; i<s.size(); i++)

        {

            if(s[i]=='1') a=i;

            else if(s[i]=='2') b=i;

            else if(s[i]=='3') c=i;

            if(a>=0&&b>=0&&c>=0)

            {

                mx=max(a,max(b,c));

                mn=min(a,min(b,c));

                ans=min(ans,(mx-mn+1));

            }


        }

        if(ans==1e9)

        cout<<"0"<<endl;

        else

        cout<<ans<<endl;

    }

}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.