본문 바로가기
C++

코딩테스트연습->전화번호 목록

by Hwoarang757 2021. 7. 20.

출처 : 프로그래머스 -> 코딩테스트 연습 - 전화번호 목록 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 전화번호 목록

전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. 전화번호가 다음과 같을 경우, 구조대 전화번호는 영석이의 전화번호의 접두사입니다. 구조

programmers.co.kr

 

###################################################################

## 2021.08.23 재작성 진행... 정확성 , 효율성 통과는 했는데 이유는 사실 정확히 파악 못했습니다 -_-;;

#######################################################################

#include <iostream>
#include <vector>
#include <string>

#include <algorithm>

using namespace std;

bool solution(vector<string> phone_book) {
	bool answer = true;

    /*
	sort(phone_book.begin(), phone_book.end(), [&](const string & a, const string & b) {
		return a.length() > b.length();
	});
    */

    sort(phone_book.begin(), phone_book.end());
    
    //bool brst=true;
    /*
	while (!phone_book.empty()) {

		string d = phone_book.front();
		phone_book.erase(phone_book.begin(),phone_book.begin()+1);
		//cout << d << endl;
        int idx=0;
		for (string& i : phone_book) {
           if(d.length() == i.length()) {
               break;
             }
			//const char* c = i.c_str();
			//cout << d.substr(0, i.length()) << endl;
			// i의 length 가 더 길다
               if( i.substr(0,d.length()) == d) {
                  answer = false;
				break;
              }        
		}
        if(!answer || !brst) break;
	}
    */
    
    
    string d,s;
    for(int i = 0 ; i < phone_book.size() ; i++) {
        d = phone_book.at(i);
        for(int j = i+1 ; j < phone_book.size() ; j++) {
            s = phone_book.at(j);
            
             if(d.length() == s.length()) {
               break;
             }
            
            // s 의 length 가 더 길다
			if (s.substr(0,d.length()) == d) {
                answer = false;
                break;
            }
        }
        if(!answer) break;
    }
    
    
    
	return answer;
}

 

 

 

 

##########################################################

## 2021.07.29 재작성 진행 ,,, 며칠째  효율성 부분에서 탈락입니다 -_-;;

########################################################

#include <string>
#include <vector>

#include <algorithm>
#include <iterator>
#include <functional>
#include <iostream>
#include <regex>
#include <cstdlib>
using namespace std;
bool solution(vector<string> phone_book);
void strCompare(string a, string b,bool &answer);

int main() {

    vector<string> test = {"9767  4223","119","1195524421","19","32892398","172732723","18","3"};

    cout << solution(test) << endl; 


}



bool solution(vector<string> phone_book) {

    bool answer = true;

    int nIdx = 0;
    string a;

    sort(phone_book.begin(), phone_book.end(), [](string a, string b) {
        return a.size() < b.size();
    });

    for (int i = 0; i < phone_book.size(); i++) {
        a = phone_book.at(i);
        nIdx++;
        int nTmp = 0;

        find_if(phone_book.begin() + nIdx, phone_book.end(), [&](const string b) {
            nTmp++;
            strCompare(a, b, answer);
            if (!answer) return true;
            else {
                if (phone_book.size() - nTmp > nTmp) {
                    strCompare(a, phone_book.at(phone_book.size() - nTmp), answer);
                    return !answer;
                }
                else
                    return true;
            }
        });
        
        if (!answer) break;
    }

     //cout << nIdx << endl;

    return answer;
}


void strCompare(string a, string b, bool& answer) {

    if (a.size() < b.size()) {
        if (b.substr(0, a.size()) == a) answer = false;
    }
    else if (a.size() > b.size()) {
        if (a.substr(0, b.size()) == b) answer = false;
    }
    else {
        if (a == b) answer = false;
    }

    //cout << a << "," << b << "=" << !answer << endl;
}

 

##########################################################

## 2021.07.21 재작성 시도 ,, 효율성 부분에서 탈락입니다 -_-;;

########################################################

#include <string>
#include <vector>

#include <algorithm>

using namespace std;

bool solution(vector<string> phone_book) {
  
    bool answer = true;
    
    int nSize = phone_book.size();
    int nIdx = 0;

    sort(phone_book.begin(), phone_book.end(), [](const string& a, const string& b) {
        if (a.length() < b.length())  return true;
        else return false;
    });

    find_if(phone_book.cbegin(), phone_book.cend(), [&](const string& str) {
        nIdx++;
        //cout << str << endl;
        for (int i = nIdx; i < nSize; i++) {
            if (str == phone_book.at(i).substr(0, str.length()) ) {
                answer = false;
                break;
            }
        }
        return !answer;
    });

    return answer;
}

 

 

#########################################################

## 2021.07.20 최초 작성 

#########################################################

#include <string>
#include <vector>

#include <algorithm>

using namespace std;

bool solution(vector<string> phone_book) {
    bool answer = true;
    
    
    int nSize = phone_book.size();
    int nIdx = 0;

    sort(phone_book.begin(), phone_book.end());
   
    find_if(phone_book.cbegin(), phone_book.cend(), [&](const string& str) {
        nIdx++;

        for (int i = nIdx; i < nSize; i++) {
            if (phone_book.at(i).find(str) != string::npos) {
                answer = false;
                //cout << str << "===============" << phone_book.at(i) << endl;
                break;
            }
                
        }
        //cout << "시도횟수=" << nIdx << endl;
        return !answer;
    });

    
    return answer;
}