This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathC_arjavgarg.c
89 lines (78 loc) · 1.58 KB
/
C_arjavgarg.c
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
#include <stdio.h>
#include <string.h>
int can_transit(char str[13], char str1[13]) {
int diff = 0;
int i;
int l1 = strlen(str);
if(l1 != strlen(str1)) {
return 0;
}
for(i = 0; i < l1; i++) {
if(str[i] != str1[i]) {
diff++;
if(diff > 1) break;
}
}
if(diff == 1) return 1;
return 0;
}
int is_diff(char str[13], char str2[13]) {
int i;
for(i = 0; i < strlen(str); i++) {
if(str[i] == str2[i]) {
return 0;
}
}
return 1;
}
int talk_to_self(char str[13], int transitions, int current, int n, char intermediate[n][n], char dic[n][13], char visited[n]) {
if(visited[current]) return 0;
visited[current] = 1;
if(is_diff(str, dic[current])) {
return transitions;
}
int i;
int min = -1;
for(i = 0; i < n; i++) {
if(intermediate[current][i] && !visited[i]) {
int x = talk_to_self(str, transitions+1, i, n, intermediate, dic, visited);
if(x){
if(min == -1) min = x;
else min = min < x? min : x;
}
}
}
if(min != -1) return min;
return 0;
}
int main() {
int n;
char s[13];
scanf("%d", &n);
scanf("%s", s);
char dic[n][13];
int i, j;
for(i = 0; i < n; i++) {
scanf("%s", dic[i]);
}
char intermediate[n][n];
char visited[n];
for(i = 0; i < n; i++) {
visited[i] = 0;
for(j = i+1; j < n; j++) {
intermediate[j][i] = intermediate[i][j] = can_transit(dic[i], dic[j]);
}
}
int min = -1;
for(i = 0; i < n; i++) {
if(can_transit(s, dic[i])) {
int x = talk_to_self(s, 0, i, n, intermediate, dic, visited);
if(x) {
if(min == -1) min = x;
else min = min > x ? x : min;
}
}
}
printf("%d", min);
return 0;
}