summaryrefslogtreecommitdiffstats
path: root/šola/aps1/dn/autocomplete/rešitev.cpp
blob: 0a5f42c9022f6ba42d5802b84ab8edf7325ea20c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
struct beseda {
	char * niz;
	long pomem;
};
static int compar_words (const void * aa, const void * bb) {
	const char * a = ((const struct beseda *) aa)->niz;
	const char * b = ((const struct beseda *) bb)->niz;
	fprintf(stderr, "compar_words: %s, %s\n", a, b);
	for (; *a && *b && *a == *b; a++, b++);
	if (*a < *b)
		return -1;
	return *a > *b;
}
static int intlog(long n) {
	int log = 1;
	for (int dolžina = 1; dolžina < n; dolžina *= 2)
		log++;
	return log;
}
int main () {
	long n;
	if (scanf("%ld", &n) == EOF) {
		perror("scanf n");
		return 1;
	}
	struct beseda * s = (struct beseda *) malloc(n*sizeof *s);
	if (!s) {
		perror("malloc s");
		return 1;
	}
	for (long i = 0; i < n; i++) {
		if (scanf("%ms %ld", &(s[i].niz), &(s[i].pomem)) == EOF) {
			perror("scanf");
			return 1;
		}
	}
	qsort(s, n, sizeof *s, compar_words);
	for (long i = 0; i < n; i++) {
		fprintf(stderr, "s[%ld].niz = \"%s\"; s[%ld].pomem = %ld;\n", i, s[i].niz, i, s[i].pomem);
	}
	int log = intlog(n);
	// fprintf(stderr, "log: %d\n", log);
	long * max = (long *) malloc((n*log)*sizeof *max);
	if (!max) {
		perror("malloc max");
		return 1;
	}
	for (int i = 0; i < log; i++) {
		for (long j = 0; j < n; j++) {
			if (i == 0) {
				max[i*n+j] = j;
				continue;
			}
			if (j+(1 << (i-1)) >= n) {
				max[i*n+j] = max[(i-1)*n+j];
				continue;
			}
			max[i*n+j] = s[max[(i-1)*n+j+(1 << (i-1))]].pomem > s[max[(i-1)*n+j]].pomem ? max[(i-1)*n+j+(1 << (i-1))] : max[(i-1)*n+j];
		}
	}
	long m;
	if (scanf("%ld", &m) == EOF) {
		perror("scanf m");
		return 1;
	}
	long * o = (long *) malloc(m*sizeof *o);
	for (long i = 0; i < m; i++) {
		char buf[20000];
		char bu2[20000];
		struct beseda prefix = {
			.niz = buf,
			.pomem = 0
		};
		struct beseda nextprefix = {
			.niz = bu2,
			.pomem = 0
		};
		if (scanf("%s", buf) == EOF) {
			perror("scanf buf");
			return 1;
		}
		fprintf(stderr, "krneki: %s\n", buf);
		strcpy(bu2, buf);
		nextprefix.niz[strlen(nextprefix.niz)-1]++;
		long l = 0, d = n;
		while (l < d) {
			long m = (l+d)/2;
			switch (compar_words(&prefix, s+m)) {
				case -1:
					d = m-1;
					break;
				case 1:
					l = m+1;
					break;
				case 0:
					l = d = m;
					break;
				default:
					assert(false);
			}
		}
		long start = l;
		fprintf(stderr, "found start = %ld;\n", start);
		l = 0, d = n;
		while (l < d) {
			long m = (l+d)/2;
			switch (compar_words(&nextprefix, s+m)) {
				case -1:
					d = m-1;
					break;
				case 1:
					l = m+1;
					break;
				case 0:
					l = d = m;
					break;
				default:
					assert(false);
			}
		}
		long end = l;
		fprintf(stderr, "found end = %ld;\n", end);
		int winsizelog = intlog(end-start)-1;
		o[i] = 0;
		if (winsizelog)
			o[i] = s[max[winsizelog*n+(end-(1 << (winsizelog-1)))]].pomem > s[max[winsizelog*n+start]].pomem ? max[winsizelog*n+start] : max[winsizelog*n+(end-(1 << (winsizelog-1)))];
		fprintf(stderr, "o[%ld] = %ld;\n", i, o[i]);
	}
	bool devica = true;
	for (long i = 0; i < m; i++) {
		if (devica)
			devica = false;
		else
			putchar(' ');
		printf("%ld", o[i]);
	}
	putchar('\n');
}