B. Ternary String----------------------------------思维(二分)

B. Ternary String----------------------------------思维(二分)_第1张图片

B. Ternary String----------------------------------思维(二分)_第2张图片

B. Ternary String----------------------------------思维(二分)_第3张图片

#include<bits/stdc++.h>
using namespace std;
int t,n;
string s;
bool check(int x)
{
	map<int,int > v;
	for(int i=0;i<x;i++) v[s[i]-'0']++;
	if(v[1]&&v[2]&&v[3]) return true;
	for(int i=1;i<n-x+1;i++)
	{
		v[s[i-1]-'0']--;
		v[s[i+x-1]-'0']++; 
		if(v[1]&&v[2]&&v[3]) return true;
	}
	return false;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s;
		 n=s.size();
		int l=0,r=n;
		int ans;
		while(l<=r)
		{
			int mid=l+r>>1;
			if(check(mid)) r=mid-1,ans=mid;
			else l=mid+1;
		}
		if(!check(ans)) cout<<0<<endl;
		else 
			cout<<ans<<endl;
	}
 } 

你可能感兴趣的:(思维,二分,Codeforces)