문제 URL : www.acmicpc.net/problem/20437
20437번: 문자열 게임 2
첫 번째 문자열에서 3번에서 구한 문자열은 aqua, 4번에서 구한 문자열은 raquator이다. 두 번째 문자열에서는 어떤 문자가 5개 포함된 문자열을 찾을 수 없으므로 -1을 출력한다.
www.acmicpc.net
문제접근법 : 알파벳대로 해당 인덱스를 vector에 저장해줍니다.
그리고 vector사이즈가 k 이상이 된다면 가장 큰길이와 작은길이를 구할수있습니다. 그것들을 찾아 출력해주면
되는문제이기에 어려운 문제는 아닙니당
소스코드 :
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
|
//By 콩순이냉장고
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
int k;
string s;
void input() {
cin >> s;
cin >> k;
}
void solve() {
vector<int>alphaidx[26];
bool flag = false;
int maxlength = -1;
int minlength = 1e8;
for (int i = 0; i < s.size(); i++) {
int idx = s[i] - 'a';
alphaidx[idx].push_back(i);
}
for (int i = 0; i < 26; i++) {
if (alphaidx[i].size() >= k) {
for (int j = 0; j <= alphaidx[i].size() - k; j++) {
maxlength = max(maxlength, alphaidx[i][j + k-1] - alphaidx[i][j] + 1);
minlength = min(minlength, alphaidx[i][j + k-1] - alphaidx[i][j] + 1);
}
}
}
if (minlength != 1e8) {
cout << minlength << " ";
cout << maxlength << "\n";
}
else
cout << -1 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int test;
cin >> test;
while (test--) {
input();
solve();
}
}
|
cs |
궁금한점 혹은 모르는 점 있다면 언제든지 댓글을 이용해주시길 바랍니당
'백준' 카테고리의 다른 글
백준 2585 경비행기 (0) | 2021.05.20 |
---|---|
백준 8983 사냥꾼 (1) | 2021.05.14 |
백준 3687 성냥개비 (0) | 2021.04.23 |
백준 17130 토끼가 정보섬에 올라온 이유 (0) | 2021.04.16 |
백준 1600 말이 되고픈 원숭이 (0) | 2021.04.05 |