|
| 1 | +1. Draw a graph |
| 2 | +The vertices are the alphabets and the edges are drawn between consecutive characters. |
| 3 | +We have to be careful about not adding the same edge twice. This is very important. |
| 4 | + |
| 5 | + for(int i = 1; i < password.size(); i++) |
| 6 | + { |
| 7 | + if(adjacency[password[i] - 'a'][password[i - 1] - 'a'] == false) |
| 8 | + { |
| 9 | + adjacency[password[i] - 'a'][password[i - 1] - 'a'] = true; |
| 10 | + adjacency[password[i - 1] - 'a'][password[i] - 'a'] = true; |
| 11 | + |
| 12 | + graph[password[i] - 'a'].push_back(password[i - 1] - 'a'); |
| 13 | + graph[password[i - 1] - 'a'].push_back(password[i] - 'a'); |
| 14 | + |
| 15 | + degree[password[i] - 'a']++; |
| 16 | + degree[password[i - 1] - 'a']++; |
| 17 | + } |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | +---- |
| 22 | + |
| 23 | +2. If any vertex has degree > 2, it is not possible as any key can only have 2 neighbours |
| 24 | + |
| 25 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 26 | + { |
| 27 | + if(degree[alpha] > 2) |
| 28 | + { |
| 29 | + cout << "NO\n"; |
| 30 | + return; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | +----- |
| 35 | + |
| 36 | +3. Now, we must look at each connected component of the graph seperately. |
| 37 | +We must find the component number of each vertex. We can do this by performing a series of DFS |
| 38 | + |
| 39 | + int component = 1; |
| 40 | + |
| 41 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 42 | + { |
| 43 | + if(component_no[alpha] == 0) |
| 44 | + { |
| 45 | + dfs(alpha, -1, component++); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | +----- |
| 50 | + |
| 51 | +Here is the DFS itself |
| 52 | + |
| 53 | +void dfs(int v, int parent_v, int c) |
| 54 | +{ |
| 55 | + //cout << "V = " << v << " C = " << c << "\n"; |
| 56 | + component_no[v] = c; |
| 57 | + |
| 58 | + for(int i = 0; i < graph[v].size(); i++) |
| 59 | + { |
| 60 | + int child_v = graph[v][i]; |
| 61 | + |
| 62 | + if(child_v == parent_v || component_no[child_v] == c) |
| 63 | + { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + dfs(child_v, v, c); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +----- |
| 72 | + |
| 73 | +4. Every component will be printed together. |
| 74 | +So, first we will print characters of component 1, then characters of component 2, and so on. |
| 75 | + |
| 76 | +----- |
| 77 | + |
| 78 | +5. How to print characters of a given component ? |
| 79 | +There have to be 2 vertices in this component of degree 1 and every other vertex must have degree 2. |
| 80 | +We will start from one of the vertices of degree 1 and keep going till we reach the vertex of degree 2 |
| 81 | + |
| 82 | +If the component is of size 1, then we can just print it |
| 83 | +Otherwise, we will look for the first border and then DFS till we reach the second border |
| 84 | + |
| 85 | +for(int c = 1; c < component; c++) |
| 86 | + { |
| 87 | + vector <int> this_component; |
| 88 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 89 | + { |
| 90 | + if(component_no[alpha] == c) |
| 91 | + { |
| 92 | + this_component.push_back(alpha); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if(this_component.size() == 1) |
| 97 | + { |
| 98 | + keyboard += (char)('a' + this_component[0]); |
| 99 | + used[this_component[0]] = true; |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + int border_1 = -1, border_2 = -1; |
| 104 | + for(int i = 0; i < this_component.size(); i++) |
| 105 | + { |
| 106 | + if(degree[this_component[i]] == 1) |
| 107 | + { |
| 108 | + if(border_1 == -1) |
| 109 | + border_1 = this_component[i]; |
| 110 | + else |
| 111 | + border_2 = this_component[i]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if(border_1 == -1 || border_2 == -1) |
| 116 | + { |
| 117 | + cout << "NO\n"; |
| 118 | + |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + keyboard += (char)('a' + border_1); |
| 123 | + used[border_1] = true; |
| 124 | + |
| 125 | + while(keyboard.back() - 'a' != border_2) |
| 126 | + { |
| 127 | + for(int i = 0; i < graph[keyboard.back() - 'a'].size(); i++) |
| 128 | + { |
| 129 | + if(!used[graph[keyboard.back() - 'a'][i]]) |
| 130 | + { |
| 131 | + int next = graph[keyboard.back() - 'a'][i]; |
| 132 | + |
| 133 | + keyboard += (char)('a' + next); |
| 134 | + |
| 135 | + used[next] = true; |
| 136 | + |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
0 commit comments