본문 바로가기

백준 문제풀이/String

[C++] 백준 문제풀이 (String) 1972번 놀라운 문자열

 

https://www.acmicpc.net/problem/1972

 

1972번: 놀라운 문자열

대문자 알파벳으로만 이루어져 있는 문자열이 있다. 이 문자열에 대해서 ‘D-쌍’이라는 것을 정의할 수 있는데, 이 문자열에 포함되어 있는, 거리가 D인 두 문자를 순서대로 나열한 것을 이 문

www.acmicpc.net

 

//[C++] 백준 문제풀이 (String)
#include <bits/stdc++.h>


bool test(std::string & s){
    if (s.length() <= 2) return true;
    int diff = 1;
    while(diff <= s.length() - 1){
        std::unordered_set<std::string> set;
        std::string str;
        for (int i = 0; i < s.length() - diff; ++i){
            str += s[i];
            str += s[i + diff];
            if (set.find(str) != set.end()) return false;
            set.insert(str);
            str.pop_back();
            str.pop_back();
        }
        ++diff;
    }
    return true;
}


int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::string s;

    while(true){
        std::cin >> s;
        if (s == "*") break;
        if (test(s)){
            std::cout << s << " is surprising.\n";
        }
        else {
            std::cout << s << " is NOT surprising.\n";
        }
    }
    return 0;
}