diff options
author | sijanec <anton@sijanec.eu> | 2021-01-04 22:24:14 +0100 |
---|---|---|
committer | sijanec <anton@sijanec.eu> | 2021-01-04 22:24:14 +0100 |
commit | 8302da73a4ef0c3d0c44c689aba9161ae8eb8d17 (patch) | |
tree | 05d6fbe3f7ee9a28a70a515d9e7905d5e93eda7f /inf/rtk/offline/resi.c | |
parent | Merge branch 'master' of https://git.šijanec.eu/sijanec/sola-gimb-2 (diff) | |
download | sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar.gz sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar.bz2 sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar.lz sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar.xz sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.tar.zst sola-gimb-2-8302da73a4ef0c3d0c44c689aba9161ae8eb8d17.zip |
Diffstat (limited to 'inf/rtk/offline/resi.c')
-rw-r--r-- | inf/rtk/offline/resi.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/inf/rtk/offline/resi.c b/inf/rtk/offline/resi.c new file mode 100644 index 0000000..43f97cc --- /dev/null +++ b/inf/rtk/offline/resi.c @@ -0,0 +1,67 @@ +#if __INCLUDE_LEVEL__ != 0 +#pragma once +#endif +/* najdaljši ponavljajoči kos niza */ +#include <stdio.h> +#include <stdlib.h> +#include <offline.h> +#include <string.h> +#define RTK_NAENKRAT 60000 +/* lahko bi šli čez niz znakov for(strlen)for(strlen)for(strlen), ampak to + * bi trajalo zelo dolgo, čas*1000000^3 in bi imelo O(n^3) kompleksnost. + * ta implementacija najde najdaljši string in ima kompleksnost + * PRIBLIŽNO OKOLI O(n^2), kar je bistveno hitreje, PRIBLIŽNO čas*1000000^2. */ +struct rtk_kos npk (const char * s, const size_t l) { + struct rtk_kos k; + k.l = 0; k.o = 0; + unsigned char ** z = calloc(l+1, sizeof(unsigned char *)); + size_t i = 0; + size_t j = 0; + for (i = 0; i < l+1; i++) + z[i] = calloc(l+1, sizeof(unsigned char)); + for (i = 1; i <= l; i++) + for (j = i+1; j <= l; j++) + if (s[i-1] == s[j-1] && z[i-1][j-1] < (j-1)) { + z[i][j] = z[i-1][j-1] + 1; + if (z[i][j] > k.l) { + k.l = z[i][j]; + k.o = k.o > i ? k.o : i; + } + } else + z[i-1][j-1] = 0; + k.o = k.o-k.l; + for (i = 0; i < l+1; i++) { + free(z[i]); z[i] = NULL; + } + free(z); + z = NULL; + return k; +} +#if __INCLUDE_LEVEL__ == 0 +int main (int argc, char ** argv) { + char c = getchar(); + size_t v = 0; + char * s = malloc(sizeof(char)*1); + struct rtk_kos k1; + struct rtk_kos k2; + size_t l = 0; + while (!feof(stdin)) { + s = realloc(s, sizeof(char)*v+2); + s[v++] = c; + s[v] = '\0'; + c = getchar(); + } + l = v; + for (v = 0; v*RTK_NAENKRAT <= l;) { + k1 = npk(s+(v*(RTK_NAENKRAT/2)), RTK_NAENKRAT); + k2 = npk(s+(++v*(RTK_NAENKRAT/2)), RTK_NAENKRAT); + } + + for (v = 0; v < k.l; v++) + putchar(s[v+k.o]); + putchar('\n'); /* za dobro mero */ + free(s); + s = NULL; + return 0; +} +#endif |