[SWEA] 7701 염라대왕의 이름 정렬

Time Lapse :18min 2sec

7701.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define gc() getchar_unlocked()
#define pc(x) putchar_unlocked(x)
#define ADD3(a,b) ((a<<3)+(a<<1)+(b&0b1111))
char buf[20000][51];
int compare(const void *A, const void *B){
int lenA = strlen((char*)A);
int lenB = strlen((char*)B);
if(lenA==lenB)
return (strcmp((char*)A,(char*)B));
return lenA > lenB;
}
int fRI(){
int N = gc(), r = 0;
for(;'0'>N||N>'9';N=gc());
do{
r = ADD3(r,N); N = gc();
}while(48<=N&&N<=57);
return r;
}
int fWI(int n){
int c = 0, r = 0;
pc('#');
while(!(n%10)) {c++; n/=10;}
while(n) {r = ADD3(r,n%10); n/=10;}
while(r) {pc(r%10|0b110000); r/=10;}
while(c--) pc(0x30);
pc(0x0A);
}
int main(void){
register int N, tc = 1, T=fRI(), len;
for(;tc<=T;tc++){
N=fRI();
for(register int i = 0 ; i < N ; i++)
scanf("%s",buf[i]);
qsort(buf,N,sizeof(buf[0]),compare);
fWI(tc);
len = 0;
while(buf[0][len]!=NULL) pc(buf[0][len++]);
pc(0x0A);
for(register int i = 1; i < N; i++)
if(strcmp(buf[i-1],buf[i])){
len = 0;
while(buf[i][len]!=NULL) pc(buf[i][len++]);
pc(0x0A);
}
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/7701/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.