A13370 | Ternary String
时间限制1s
内存限制256MB
通过 / 提交0/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$ .
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 \le t \le 20000$ ) — the number of test cases.
Each test case consists of one line containing the string $s$ ( $1 \le |s| \le 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$ .
Each test case consists of one line containing the string $s$ ( $1 \le |s| \le 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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted