-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEC87-B.cpp
More file actions
55 lines (50 loc) · 1.29 KB
/
Copy pathEC87-B.cpp
File metadata and controls
55 lines (50 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Educational Codeforces Round 87 (Rated for Div. 2), problem: (B) Ternary String
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int findSubString(string str, string pat)
{
ll len1 = str.length();
ll len2 = pat.length();
if (len1<len2) {
return 0;
}
ll hash_pat[256] = {0};
ll hash_str[256] = {0};
for(ll i=0; i<len2; i++)
hash_pat[pat[i]]++;
ll start=0,index = -1, len = INT_MAX, count = 0;
for (ll j = 0; j < len1 ; j++){
hash_str[str[j]]++;
if (hash_pat[str[j]] != 0 && hash_str[str[j]] <= hash_pat[str[j]] )
count++;
if (count == len2){
while ( hash_str[str[start]]>hash_pat[str[start]] || hash_pat[str[start]] == 0){
if (hash_str[str[start]]>hash_pat[str[start]])
hash_str[str[start]]--;
start++;
}
int len_window= j-start+1;
if (len>len_window){
len=len_window;
index=start;
}
}
}
if (index<0){
return 0;
}
return len;
}
int main(){
ll t;
string str = "";
string pat = "123";
cin>>t;
while(t--){
cin>>str;
cout <<findSubString(str, pat)<<"\n";
}
return 0;
}