社区讨论

求助优化?这都过不去?

CF1354B Ternary String参与者 4已保存回复 8

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
8 条
当前快照
1 份
快照标识符
@lo1x06h6
此快照首次捕获于
2023/10/23 04:22
2 年前
此快照最后确认于
2023/11/03 04:49
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
#define re register
#define int long long
const int N=20005,NN=200005,inf=1<<29;

int T;

string s;
struct node{
	int one,two,th;
	
	node(){one=0,two=0,th=0;}
	
	node operator + (const node &a){
		node res;
		
		res.one=one+a.one;
		res.two=two+a.two;
		res.th=th+a.th;
		
		return res;
	}
}sum[NN];

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	cin>>T;
	while(T--){
		int tmp=0,f=0,ans=inf;
		string s;memset(sum,0,sizeof(sum));
		
		cin>>s;
		for(re int i=0;i<s.length();i++){
			if(s[i]=='1')
				sum[i+1].one++;
			if(s[i]=='2')
				sum[i+1].two++;
			if(s[i]=='3')
				sum[i+1].th++;
			sum[i+1]=sum[i+1]+sum[i];
			
			if(sum[i+1].one&&sum[i+1].two&&sum[i+1].th&&(!f||s[i]!=s[i-1])){
				if(s[tmp-1]!=s[i])
					ans=min(ans,i-tmp+1);
				f=1;
			}
			if(s[i]!=s[i-1])
				tmp=i;
		}
		if(ans>=inf)
			cout<<"0\n";
		else
			cout<<ans+1<<"\n";
	}
	
	return 0;
}

回复

8 条回复,欢迎继续交流。

正在加载回复...