-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_.c
More file actions
84 lines (69 loc) · 2.12 KB
/
code_.c
File metadata and controls
84 lines (69 loc) · 2.12 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1501
typedef struct {
char key[20];
int row;
int col;
} hash_;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d %d", &n, &m);
char arr[n][m][20]; // 2D array of strings (each with max 19 chars + null terminator)
// Input the grid of strings
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%s", arr[i][j]);
}
}
hash_ hashmap[MAX_SIZE];
int hash_count = 0;
// Build the hashmap
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
strcpy(hashmap[hash_count].key, arr[i][j]); // Correctly copy the string into hashmap
hashmap[hash_count].row = i;
hashmap[hash_count].col = j;
hash_count++;
}
}
int q;
scanf("%d", &q);
// Process each query
while (q--) {
int row_ = -1;
char a[20], b[20];
scanf("%s %s", a, b);
// Find the row where string `a` is located
for (int i = 0; i < hash_count; i++) {
if (strcmp(hashmap[i].key, a) == 0) { // Compare entire strings
row_ = hashmap[i].row;
break; // Exit once the row is found
}
}
// If no matching row was found, print -1
if (row_ == -1) {
printf("-1\n");
continue;
}
// Now search for string `b` in the row `row_`
int found = 0;
for (int j = 0; j < m; j++) {
if (strcmp(b, arr[row_][j]) == 0) { // Compare entire strings
printf("%d ", row_);
found = 1;
break; // Exit once the string `b` is found
}
}
if (!found) {
printf("-1 ");
}
}
printf("\n");
}
return 0;
}