c++ 기본기 없이 코딩하려니 에러도 많이나서 질문도 많이했다.
풀이과정은
1. 무식하게 버블정렬 : 시간초과
2. sort를 사용하니까 통과됨
bool cmp(string a, string b){
if(a.length() < b.length()) return true;
else if(a.length() == b.length()) return a.compare(b) < 0;
return false;
}
a.length >= b.length를 고려 안해줘서 조금 많이 틀렸다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <vector> | |
#include <stdlib.h> | |
#include <string> | |
#include <string.h> | |
#include <algorithm> | |
/** | |
* https://www.acmicpc.net/problem/1181 | |
* BOJ 백준온라인져지 1181 단어 정렬 풀이 | |
*/ | |
using namespace std; | |
bool cmp(string a, string b){ | |
if(a.length() < b.length()) return true; | |
else if(a.length() == b.length()) return a.compare(b) < 0; | |
return false; | |
} | |
int main(){ | |
int N; | |
vector<string> v = vector<string>(); | |
scanf("%d",&N); | |
while(N--){ | |
char *temp = (char*)malloc(60); | |
scanf("%s",temp); | |
string t = string(temp); | |
if(find(v.begin(), v.end(), t) == v.end()){ | |
v.push_back(t); | |
} | |
free(temp); | |
} | |
sort(v.begin(), v.end(), cmp); | |
for(int i = 0,size=v.size(); i<size; i++) | |
printf("%s\n",v[i].c_str()); | |
return 0; | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 9020 골드바흐의 추측 풀이 (0) | 2017.11.25 |
---|---|
BOJ 백준온라인져지 4948 베르트랑 공준 풀이 (0) | 2017.11.24 |
BOJ 백준온라인져지 1427 소트인사이드 풀이 (0) | 2017.11.22 |
BOJ 백준온라인져지 9426 중앙값 측정 풀이 (0) | 2017.11.21 |
BOJ 백준온라인져지 1786 찾기 풀이 (1) | 2017.11.19 |