This repository was archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlista2.cpp
More file actions
123 lines (104 loc) · 2.87 KB
/
lista2.cpp
File metadata and controls
123 lines (104 loc) · 2.87 KB
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
/* Organizando os arquivos
https://iudex.io/problem/5b91a3b3fcc3820001202248/statement */
#include <bits/stdc++.h>
using namespace std;
// ~~~ NODE ~~~
struct node {
string nome;
int id;
node *next;
// CONSTRUTOR DO NODE
node(string nome, int id): nome(nome), id(id), next(nullptr) {}
};
// ~~~ GAVETA ~~~
struct gaveta {
node *front;
node *rear;
int tam;
// CONSTRUTOR DA GAVETA
gaveta (): front(nullptr), rear(nullptr), tam(0) {}
void add (string nome, int id){
node *aux = new node (nome, id);
if (this->rear){
this->rear->next = aux;
this->rear = aux;
}else{
this->front = aux;
this->rear = aux;
}
tam++;
}
int procurar (string nome){
node *aux = this->front;
int cont = 0;
while (aux->next){
if (aux->nome.compare(nome)==0){
return cont;
}
}
return -1;
}
int acessoTam (){
return this->tam;
}
};
int buscaBinaria (int ids[], int quantArquivos, int idProcurado){
int iniAux = 0;
int fimAux = quantArquivos-1;
while (iniAux<=fimAux){
int posMeio = (iniAux+fimAux)/2;
if (idProcurado == ids[posMeio]){
return posMeio;
}else if (idProcurado<ids[posMeio]){
fimAux = posMeio-1;
}else{
iniAux = posMeio+1;
}
}
return -1;
}
int dispersao (string nome, int quantGavetas){
int chave = 0;
for (int i=0; i<nome.length(); i++){
char letrinha = nome[i];
chave+=(i*letrinha);
}
return (chave%quantGavetas);
}
int main (){
int quantArquivos;
cin >> quantArquivos;
string arquivosIniciaisNomes [quantArquivos];
int arquivosIniciaisIds [quantArquivos];
for (int i=0; i<quantArquivos; i++){
string nome;
cin >> nome;
arquivosIniciaisNomes[i] = nome;
int id;
cin >> id;
arquivosIniciaisIds[i] = id;
}
int quantGavetas;
cin >> quantGavetas;
gaveta gavetas[quantGavetas];
int quantArqTransf;
cin >> quantArqTransf;
for (int i=0; i<quantArqTransf; i++){
int idProcurado;
cin >> idProcurado;
int posEncontrada = buscaBinaria(arquivosIniciaisIds, quantArquivos, idProcurado);
int posGavetas = dispersao(arquivosIniciaisNomes[posEncontrada], quantGavetas);
gavetas[posGavetas].add(arquivosIniciaisNomes[posEncontrada], idProcurado);
}
for (int i=0; i<quantGavetas; i++){
cout << i << ": " << gavetas[i].acessoTam() << endl;
}
int quantArqConsult;
cin >> quantArqConsult;
for (int i=0; i<quantArqConsult; i++){
string nomeProcurado;
cin >> nomeProcurado;
cout << i << ": " << gavetas[dispersao(nomeProcurado, quantGavetas)].procurar(nomeProcurado) << endl;
}
return 0;
}