Ternary String CodeForces - 1354B(尺取法)

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
Input
7
123
12222133333332
112233
332211
12121212
333333
31121
Output
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.
思路:挺明显的尺取,两个指针来回动就可以了,注意边界条件。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

int vis[100];
string s;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		cin>>s;
		int ans=inf;
		int l=0,r=0;
		int len=s.length();
		memset(vis,0,sizeof(vis));
		int sum=0;
		while(r<len)
		{
			while(r<len)
			{
				if(vis[s[r]-'0']==0) sum++;
				vis[s[r]-'0']++;
				if(sum==3) break;
				r++;
			}
			if(r<len) ans=min(ans,r-l+1);
			while(sum==3)
			{
				vis[s[l]-'0']--;
				if(vis[s[l]-'0']==0) sum--;
				l++;
				if(sum==3) ans=min(ans,r-l+1);
			}
			r++;
		}
		if(ans==inf) cout<<0<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个警告是由ESLint的规则`no-unneeded-ternary`引起的,它表示在默认赋值时使用条件表达式是不必要的。 在上面的代码示例中,可以看到在`filterItems`方法中使用了条件表达式来处理空字符串的情况。然而,根据该警告,使用条件表达式是不必要的,因为我们可以直接使用简单的赋值操作来实现相同的效果。 要解决这个警告,你可以将条件表达式改为简单的赋值操作。 下面是修改后的代码示例: ```vue <template> <div> <q-select v-model="selectedItem" :options="filteredItems" option-label="label" option-value="value" :filter="filterItems" input-debounce="300" placeholder="Search" /> </div> </template> <script> export default { data() { return { selectedItem: '', items: [ { label: 'Option 1', value: 'option1' }, { label: 'Option 2', value: 'option2' }, { label: 'Option 3', value: 'option3' }, // 其他选项... ], filteredItems: [] }; }, methods: { filterItems(val, update) { if (val === '') { update(() => { this.filteredItems = []; }); } else { const lowercaseVal = val.toLowerCase(); this.filteredItems = this.items.filter(item => item.label.toLowerCase().includes(lowercaseVal) ); } } } }; </script> ``` 在修改后的代码中,我们移除了条件表达式,并直接在`if`语句的分支中进行赋值操作。这样可以避免不必要的条件表达式。 通过这种方式,我们可以消除ESLint警告,并且代码逻辑仍然保持一致。 希望这个解决方案对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值