Pagini recente » Cod sursa (job #2371323) | Cod sursa (job #2573127) | Cod sursa (job #863901) | Cod sursa (job #747605) | Cod sursa (job #625316)
Cod sursa(job #625316)
#include <cstdio>
#include <cstring>
struct Nod {
int cuv;
Nod *f[10];
Nod() {
cuv = 0;
for (int i = 0; i < 10; ++i)
f[i] = NULL;
}
};
const int N = 100005;
int n, aib[N];
Nod *trie[N];
char s[N];
int bit(int x) {
return (x & (-x));
}
// update the aib
void update(int poz) {
while (poz < N) {
++aib[poz];
poz += bit(poz);
}
}
// query on the aib
int query(int poz) {
int rez = 0;
while (poz) {
rez += aib[poz];
poz -= bit(poz);
}
return rez;
}
// insert a new word in trie
void insert(Nod *nc, int poz) {
++nc -> cuv;
if (poz == (int)strlen(s))
return;
if (nc -> f[s[poz] - '0'])
insert(nc -> f[s[poz] - '0'], poz + 1);
else {
nc -> f[s[poz] - '0'] = new Nod();
insert(nc -> f[s[poz] - '0'], poz + 1);
}
}
// search the pozth number from the current trie
void search(Nod *nc, int poz) {
for (int i = 0; i < 10; ++i)
if (nc -> f[i] && nc -> f[i] -> cuv < poz)
poz -= nc -> f[i] -> cuv;
else if (nc -> f[i]) {
printf("%d", i);
search(nc -> f[i], poz);
break;
}
}
// search for the length of the answer
int binary_search(int val) {
int i = 0, pas = (1 << 17);
for (i = 0; pas; pas >>= 1)
if (i + pas < N && query(i + pas) < val)
i += pas;
return i + 1;
}
int main() {
freopen("nums.in", "r", stdin);
freopen("nums.out", "w", stdout);
int type, poz, trc, lun;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d ", &type);
if (type == 1) {
gets(s);
lun = strlen(s);
if (!trie[lun])
trie[lun] = new Nod();
insert(trie[lun], 0);
update(strlen(s));
} else {
scanf("%d\n", &poz);
trc = binary_search(poz);
search(trie[trc], poz - query(trc - 1));
printf("\n");
}
}
return 0;
}