#define _GNU_SOURCE #include #include #include #include #define CHUNK_SIZE 40000000 #define LINES_CHUNK_SIZE 10000 #define BUF_SIZE 320000 void dump(char *buffer, int size) { while (size>0) { int w = write(1, buffer, size); buffer+=w; size-=w; } } static inline char * dumpstr(char *buffer, char *current, char *word, int len, int newline) { int capacity = buffer+BUF_SIZE-current; if (len > capacity) { dump(buffer, current - buffer); current = buffer; } if (len>8) { memcpy(current,word,len); } else { memcpy(current,word,8); } /* switch (len) { case 0: break; case 1: memcpy(current,word,1);break; case 2: memcpy(current,word,2);break; case 3: memcpy(current,word,3);break; case 4: memcpy(current,word,4);break; case 5: memcpy(current,word,5);break; case 6: memcpy(current,word,6);break; case 7: memcpy(current,word,7);break; case 8: memcpy(current,word,8);break; default: memcpy(current,word,len);break; }*/ current+=len; if (newline) { *current = '\n'; current++; } return current; } int main() { // INPUT reading char *buf = malloc(CHUNK_SIZE); int size = CHUNK_SIZE; { char *s = buf; int left = CHUNK_SIZE; while(1){ int n = read(0, s, left); if(n == 0) break; s += n; left -= n; if(left == 0) { int tmp = size; size += CHUNK_SIZE; left = CHUNK_SIZE; buf=realloc(buf, size); s = buf + tmp; } } *s = '\n'; *(s+1) = '\0'; //unlikely if (size < s-buf+8) { buf = realloc(buf, size+8); } } char **w = NULL; ssize_t *o = NULL; int k = 0; { char *s = buf; while (*s) { if (k % LINES_CHUNK_SIZE == 0) { w = realloc(w, (k+LINES_CHUNK_SIZE)*sizeof(ssize_t)); o = realloc(o, (k+LINES_CHUNK_SIZE)*sizeof(ssize_t)); } w[k] = s; // set offset while (*s != '\r' && *s != '\n' && *s != ' ') s++; // read word o[k] = s-w[k]; // set length k++; while (*s == '\r' || *s == '\n' || *s == ' ') s++; // read space characters } } char out_buf[BUF_SIZE+8]; char *s = out_buf; for(int i = 0; i < k; i+=2) for(int j = 1; j < k; j+=2){ s=dumpstr(out_buf,s,w[i],o[i],0); s=dumpstr(out_buf,s,w[j],o[j],1); } dump(out_buf, s - out_buf); return 0; }