import sys
input=sys.stdin.readline
T=int(input())
for _ in range(T):
alpha=list(input().strip())
history = [[] for _ in range((ord('z') - ord('a') + 1))] # 알파벳 배열
for i in range(len(alpha)):
history[ord(alpha[i])-ord('a')].append(i) #index저장
K=int(input())
short,long=10000,0
for i in range(len(history)):
if len(history[i])>=K:
for j in range(0,len(history[i])-K+1):
tmp=history[i][j+K-1]-history[i][j]+1 # a..a 총 k개를 포함한 문자열 길이
short=min(short,tmp)
long=max(long,tmp)
if short==10000 and long==0: print(-1)
else: print(short,long)
https://www.acmicpc.net/problem/20437
20437번: 문자열 게임 2
첫 번째 문자열에서 3번에서 구한 문자열은 aqua, 4번에서 구한 문자열은 raquator이다. 두 번째 문자열에서는 어떤 문자가 5개 포함된 문자열을 찾을 수 없으므로 -1을 출력한다.
www.acmicpc.net
'IT > coding study' 카테고리의 다른 글
[acmicpc] 21921. 블로그 (python) (0) | 2022.10.04 |
---|---|
[acmicpc] 17822. 원판 돌리기 (python) (1) | 2022.09.30 |
[acmicpc] 1806. 부분합(python) (2) | 2022.09.21 |
[acmicpc] 1987. 알파벳(python) (0) | 2022.09.19 |
[acmicpc] 2467. 용액(python) (0) | 2022.09.19 |