Source Code
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define TEST int t;cin>>t;while(t--)
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fr first
#define sc second
#define popcount __builtin_popcountll
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const ll mod=1000000007;
typedef vector<int,int> p1;
typedef vector<ll,ll> p2;

int main()
{
    IOS;
    //TEST{

  int n;
  cin>>n;
  string s;
  cin>>s;
  int id1=0,id2=n-1,mi=1e9;
    char a=s[0],b=s[n-1];
    int cur=a-'0'+b-'0';
    string str=s;
    int cc=n;
    if(cur==0){rep(i,cc){ if(str[i]=='0'){mi=min(mi,i-id1); id1=i;} }}
    if(cur==2){rep(i,cc){ if(str[i]=='1'){mi=min(mi,i-id1); id1=i;} }}
    if(cur==1){rep(i,cc){ if(str[i]==a)id1=i; if(str[i]==b){mi=min(mi,i-id1);} }}

    cout<<n-(mi+1);
cout<<endl;
//}
    return 0;
}
Copy
Pop Pop AlCoach
GNU G++17
1 ms
976 KB
Accepted