#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
/* usage: sufsort1 text > text.suf */
char *text;
suffix_compare(int *a, int *b)
{
return strcmp(text + *a, text + *b);
}
main(int ac, char **av)
{
struct stat stat_buf;
int N, i, *suf;
FILE *fd = fopen(av[1], "r");
fstat(fileno(fd), &stat_buf);
N = stat_buf.st_size;
text = (char *)malloc(N+1);
fread(text, sizeof(char), N, fd);
text[N] = 0; /* pad with null */
suf = (int *)malloc(N * sizeof(int));
for(i=0;i<N;i++) suf[i] = i;
qsort(suf, N, sizeof(int), suffix_compare);
fwrite(suf, N, sizeof(int), stdout);
}
(http://ta2o.net/doc/pub/dron-sa.pdf)改行文字も含めた各行の平均バイト数が15だとすると全体で150万バイト(1.5M)のテキストデータとなる。000001 六本木 000002 呼吸 ... 099999 鰻 100000 札幌
000001 六本木 000002 呼吸 ... 099999 鰻 100000 札幌 000001 六本木 000002 呼吸 ... 099999 鰻 100000 札幌
または、ついでに重複除去して、sort original.txt > new.txt
をコマンドラインで実行して、sort -u original.txt > new.txt