출처 : 프로그래머스 -> 코딩테스트 연습 - 전화번호 목록 | 프로그래머스 (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;
}
'C++' 카테고리의 다른 글
[C++/WinRt] Windows Toolkit - Windows.Data.Pdf 를 이용한 PDF To Bitmap 변환 예제 (0) | 2024.07.29 |
---|---|
코딩테스트 연습->탐욕법(Greedy)->체육복 (0) | 2021.09.01 |
코딩테스트 연습->해시->위장 (0) | 2021.08.30 |
코딩테스트연습->정렬->K번째수 (0) | 2021.07.22 |
코딩테스트 연습 -> 중복된 이름 제거 (0) | 2021.07.08 |