프로그래머스 코테 고득점 kit / lv. 2 / 전화번호 목록 / C++ [해시]

jjin·2023년 10월 6일
0

container크기는 length아니고 size

s1.find(s2) != s1.end() // found

s1.substr(i,j+1)

정렬 - 백트래킹을 곁들인

조금 더 난이도가 있는데 정렬을 했을 때 바로 뒤랑만 비교하면 된다는 것을 떠올려야하기 때문이다. (promise)
바로 뒤가 다르면 그 이상은 같을 수가 없다.


#include <bits/stdc++.h>
using namespace std;

/*
사전순 정렬 후 find 결과가 begin인게 존재하면 true
*/

bool solution(vector<string> p) {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    
    sort(p.begin(), p.end());
    int l = p.size();
    for (int i = 0; i < l-1; i++) {
        if (p[i+1].find(p[i]) == 0)
            return false;
    }
    return true;
}

해시

#include <bits/stdc++.h>
using namespace std;

/*
큰 문자를 잘라나가며 있는지 확인
*/

bool solution(vector<string> p) {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    
    unordered_map<string, bool> m;
    
    for (int i = 0; i < p.size(); i++) 
        m[p[i]] = true;
    
    for (string e : p){
        for (int i = 0; i < e.length() - 1; i++){
            if (m[e.substr(0, i+1)])
                return false;
        }
    }
    return true;
}
profile
진짜

0개의 댓글