Skip to content

Instantly share code, notes, and snippets.

@cls cls/suffix-array.c
Last active Dec 30, 2017

Embed
What would you like to do?
Constructing a simple suffix array
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int pstrcmp(const void *, const void *);
int
main(void)
{
char buf[BUFSIZ];
if (!fgets(buf, sizeof buf, stdin))
return 1;
size_t len = strlen(buf);
const char *array[len];
for (size_t i = 0; i < len; i++)
array[i] = &buf[i];
qsort(array, len, sizeof *array, pstrcmp);
for (size_t i = 0; i < len; i++)
printf("%tu\n", array[i] - buf);
return 0;
}
int
pstrcmp(const void *p1, const void *p2)
{
const char *const *s1 = p1;
const char *const *s2 = p2;
return strcmp(*s1, *s2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.