Generating all unique substring of a string.

Here is the code to generate unique substring , using suffix array.

#include <iostream>
#include <string.h>
#include <vector>
#include <string>
#include <algorithm>

char txt[2000], *p[2000];
int m, n;

int cmp(const void *p, const void *q) {
    int rc = memcmp(*(char **)p, *(char **)q, m);
    return rc;
}
int main() {
    std::cin >> txt;
    n = strlen(txt);
    int k; int i;
    for (m = 1; m <= n; m++) {
        for (k = 0; k+m <= n; k++)
            p[k] = txt+k;
        qsort(p, k, sizeof(p[0]), &cmp);
        for (i = 0; i < k; i++) {
            if (i != 0 && cmp(&p[i-1], &p[i]) == 0){
                continue;
            }
            char cur_txt[2000];
            memcpy(cur_txt, p[i],m);
            cur_txt[m] = '\0';
            std::cout << cur_txt << std::endl;
        }
    }
    return 0;
}
Advertisements

One thought on “Generating all unique substring of a string.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s