一千题,No.0026(Ternary String)

描述

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.

输入描述

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.

输出描述

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.

用例输入 1 

7
123
12222133333332
112233
332211
12121212
333333
31121

用例输出 1 

3
3
4
4
0
0
4

提示

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这样,它的每个字符要么是 1、2 还是 3。您必须选择最短的连续子字符串s这样它就至少包含这三个字符中的每一个一次。

字符串的连续子字符串s是一个字符串,可以从s通过从开头删除一些(可能为零)字符s以及末尾的一些(可能为零)字符s.

输入描述

第一行包含一个整数t (1≤≤20000) — 测试用例的数量。

每个测试用例都由一行包含字符串s (1≤∣∣≤20000 0).保证每个字符s是 1、2 或 3。

所有测试用例中所有字符串的长度总和不超过20000 0.

输出描述

对于每个测试用例,打印一个整数 — 最短连续子字符串的长度s至少包含一次所有三种类型的字符。如果没有这样的子字符串,请打印0相反。

用例输入 1 

7
123
12222133333332
112233
332211
12121212
333333
31121

用例输出 1 

3
3
4
4
0
0
4

提示

请看示例测试:

在第一个测试用例中,可以使用子字符串 123。

在第二个测试用例中,可以使用子字符串213。

在第三个测试用例中,可以使用子字符串 1223。

在第四个测试用例中,可以使用子字符串 3221。

在第五个测试用例中,没有字符 3s.

在第六个测试用例中,没有字符 1s.

在第七个测试用例中,可以使用子字符串 3112。

解题思路:

1,将数字存储到string中

2,写出函数判断是否存在123这3个数,如果不存在直接输出0

3,循环出字符串>3的所有可能并用2的函数判断,然后比较迭代最小字符数

c++代码如下:

#include <bits/stdc++.h>

using namespace std;

bool is_ok(string s)
{
    int a = 0,b = 0,c = 0;
    for(int i = 0;i < s.size();++i)
    {
        char cc = s[i];
        if(cc == '1') ++a;
        if(cc == '2') ++b;
        if(cc == '3') ++c;
        if(a > 0 && b > 0 && c > 0)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        bool b = false;
        int res = 20005;
        string s;
        cin >> s;
        if(s.size()<3 || !is_ok(s))
        {
            cout << 0 << endl;
        }
        else
        {
            int i = 0,j = 0;
            for(i = 3;i <= s.size()-j;++i)
            {
                if(b)
                {
                    break;
                }
                for(j = 0;j<=s.size();++j)
                {
                    string new_s = s.substr(j,i);
                    if(is_ok(new_s))
                    {
                        b = true;
                        res = new_s.size();
                        break;
                    }
                }
            }

            /*
            for(int i = 0;i <= s.size()-3;++i)
            {
                for(int j = 3;j <= s.size()-i;++j)
                {
                    string new_s = s.substr(i,j);
                    if(is_ok(new_s))
                    {
                        b = true;
                        if(res > new_s.size())
                        {
                            res = new_s.size();
                        }
                    }
                }
            }
             */


            if(b)
            {
                cout << res << endl;
            }
            else
            {
                cout << 0 << endl;
            }
        }
    }
}

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值