USACO/Bronze

백준 18269 Where Am I? (USACO December 2019 Bronze 2번)

ssam.. 2020. 3. 20. 21:52

문제 링크: https://www.acmicpc.net/problem/18269

 

18269번: Where Am I?

The first line of input contains $N$, and the second line contains a string of $N$ characters, each in the range A..Z.

www.acmicpc.net

예시

7

ABCDABC

연속된 세개의 letter로는 unique한 위치를 결정할수 없음. (예를들어 "ABC"가 두개의 다른 위치에 존재함). 하지만 연속의 네개의 letter로는 unique한 위치를 결정할수 있기 때문에 답은 4임. 1부터 가능한지 체크해서, 가능한 순간의 갯수를 출력하고 프로그램을 종료시키면 됨.

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
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    string s;
    cin >> n >> s;
 
    for(int k = 1; k <= n; k++) {
        bool ok = true;
        for(int i = 0; i <= n - k; i++) {
            string tmp = s.substr(i, k);
            for(int j = 0; j <= n - k; j++) {
                if(i == j) continue;
                if(tmp == s.substr(j, k)) ok = false;
            }
        }
        if(ok) {
            cout << k << '\n';
            return 0;
        }
    }
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter