B. Ternary String

5 篇文章 0 订阅

You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once.

A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s.

Input
The first line contains one integer t (1≤t≤20000) — the number of test cases.

Each test case consists of one line containing the string s (1≤|s|≤200000). It is guaranteed that each character of s is either 1, 2, or 3.

The sum of lengths of all strings in all test cases does not exceed 200000.

Output
For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead.

Example
inputCopy
7
123
12222133333332
112233
332211
12121212
333333
31121
outputCopy
3
3
4
4
0
0
4
Note
Consider the example test:

In the first test case, the substring 123 can be used.

In the second test case, the substring 213 can be used.

In the third test case, the substring 1223 can be used.

In the fourth test case, the substring 3221 can be used.

In the fifth test case, there is no character 3 in s.

In the sixth test case, there is no character 1 in s.

In the seventh test case, the substring 3112 can be used.

题意
给你字符串s,s由字符’1’,‘2’,'3’组成,找到一个长度最短的区间,该区间至少包含3种字符,输出这个最短的长度
思路
标签里贴了二分,但是要用前缀和优化一下(我没用前缀和的T了,但是我懒得改orz)
这个题还可以用尺取,可以直接尺取模板套上去(55555寒假学的尺取又忘掉了)
尺取代码

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
//#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
string s;
map<char,int>vis;//统计字符'1','2','3'在区间内的次数

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		vis.clear();
		cin >> s;
		int sum, l, r;
		sum = l = r = 0;
		int d = 20000000;
		while (1)
		{
			while (sum < 3 && r < s.size())
			{
				if (vis[s[r]] == 0) sum++;
				vis[s[r]]++;
				r++;
			}
			if (sum < 3)
			{
				break;
			}
			d = min(d, r - l);
			vis[s[l]]--;
			if (vis[s[l]] == 0) sum--;
			l++;
		}
		if (d > s.size()) d = 0;
		cout << d << endl;
	}

}

贴一个TLE的二分昂~

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
//#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
string s;
int len;
bool valid(string u)
{
	int len = u.size();
	int cnt1 = 0, cnt2 = 0, cnt3 = 0;
	for (int i = 0; i < len; i++)
	{
		if (u[i] == '1') cnt1 = 1;
		else if(u[i]=='2') cnt2=1;
		else if (u[i] == '3') cnt3 = 1;
		if (cnt1&&cnt2&&cnt3) return true;
	}
	return false;
}
bool check(int x)
{
	
	for (int i = 0; i + x - 1 < len; i++)
	{
		if (valid(s.substr(i, x))) return true;
	}
	return false;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		
		cin >> s;
		len = s.size();
		if (valid(s))
		{
			int l = 0, r = len;
 
			while (l <= r)
			{
				int mid = (l + r) >> 1;
				if (check(mid))
				{
					r = mid - 1;
				}
				else
				{
					l = mid + 1;
				}
			}
			cout << r + 1 << endl;
		}
		else cout << 0 << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值