blob: 43f97cc9f45ea5c20f099f6f5cc7b1e7b4aa7887 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
|