단어의 개수가 2만개라서, 이 방법으로는 시간안에 정렬할 수 없습니다.
1181번 - 단어 정렬
qsort 이용해서 풀었습니다 감사합니다~!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare(const void *a, const void *b);
int main(void) {
int num;
int i;
scanf("%d",&num);
char word[num][51]; //각 단어 행에 하나씩 저장
for(i=0;i<num;i++){
scanf("%s",word+i);
}
qsort(word,num,sizeof(word[0]),compare);
for(i=0;i<num;i++){ //같은 단어는 하나만 나오게 출력
if(!strcmp(word+i,word+i+1)){
continue;
}else{
printf("%s\n",word+i);
}
}
return 0;
}
int compare(const void *a, const void *b){
int a_len;
int b_len;
a_len = strlen(a);
b_len = strlen(b);
if(a_len == b_len){
return strcmp((char*)a, (char*)b);
}else if(a_len > b_len){
return 1;
}else{
return -1;
}
}
댓글을 작성하려면 로그인해야 합니다.
ardorhth39 6년 전
몇 가지 예제를 넣어보면 맞는 것 같은데
시간초과라고 뜹니다. 어떤 부분에서 오래 걸리는 것일까요??
틀린 부분이 있거나 시간이 왜 오래 걸리는지 알려주시면 감사하겠습니다~!!