What are the states?
Should we use "brute force"?
(e.g. enumerate and evaluate millions of guesses)
Is there any insight or leverage we could exercise?
What is the easiest way to "compare" 2 combinations?
X * (X - 1) * (X - 2) ... [Y terms worth] / Y !Consider: all combinations of 6 things taken 3 at a time
6 * 5 * 4 / 3 ! = 6 * 5 * 4 / 3 * 2 * 1 = 20How can we produce all these combinations?
In the output below, most of the combinations have repeated digits ('x') or are duplicates ('d').
Notice that the first legal combinations in each group are: 123, and 234.
How can we fix this approach to only yield "legal" combinations?
int main() { int array[] = { 1, 2, 3, 4, 5, 6 }; for (int i=0; i < 2; ++i) { for (int j=0; j < 6; ++j) { for (int k=0; k < 6; ++k) cout << array[i] << array[j] << array[k] << ' '; cout << '\n'; } cout << '\n'; } } // x x x x x x x x d d d d // 111 112 113 114 115 116 211 212 213 214 215 216 // x x x x x x x x // 121 122 123 124 125 126 221 222 223 224 225 226 // x d x d x x // 131 132 133 134 135 136 231 232 233 234 235 236 // x d d x d x d x // 141 142 143 144 145 146 241 242 243 244 245 246 // x d d d x d x d d x // 151 152 153 154 155 156 251 252 253 254 255 256 // x d d d d x d x d d d x // 161 162 163 164 165 166 261 262 263 264 265 266
How about adapting the inner loops to not "visit territory" already visited by outer loops
("j = i + 1" and "k = j + 1"),and instrumenting the outer loops to not duplicate work reserved for inner loops
("i < 4" and "j < 5")?
int main() { int array[] = { 1, 2, 3, 4, 5, 6 }; for (int i=0; i < 4; ++i) { for (int j=i+1; j < 5; ++j) for (int k=j+1; k < 6; ++k) cout << array[i] << array[j] << array[k] << ' '; cout << '\n'; } } // 123 124 125 126 134 135 136 145 146 156 // 234 235 236 245 246 256 // 345 346 356 // 456 // 6 * 5 * 4 / 3 * 2 * 1 = 20
Given the formula for "all combinations of X things taken Y at a time",
What is the total number of combinations of 50 states taken 2 at a time?
int main() { int count = 0; for (int i=0; i < 49; ++i) for (int j=i+1; j < 50; ++j) count++; cout << "total combinations is " << count << '\n'; } // total combinations is 1225 // 50 * 49 / 2 * 1
Let's consider how to handle the second step.
What is the easiest way to "compare" a 2-state combination with another 2-state combination?Do we want to step through the characters of one combination, and find and check off each character in the other combination until a mismatch is identified?
What about converting each combination to some canonical form that could reduce the comparison to a simple string compare?
How about sorting each combination before comparing?
int main() { string array[] = { "red", "dare", "dear", "deer", "read", "road", "dread" }; string sorted; for (int i=0; i < 7; ++i) { sorted = array[i]; sort( sorted.begin(), sorted.end() ); cout << sorted << '\t' << array[i] << '\n'; } } // der red // ader dare // ader dear // deer deer // ader read // ador road // adder dread
The strategy of sorting each string makes it easy to identify the 3 words composed of identical characters.
string states[] = { "alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "newhampshire", "newjersey", "newmexico", "newyork", "northcarolina", "northdakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhodeisland", "southcarolina", "southdakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "westvirginia", "wisconsin", "wyoming" }; int main() { string pairing, sorted; for (int i=0; i < 3; ++i) for (int j=i+1; j < 4; ++j) { sorted = pairing = states[i] + states[j]; sort( sorted.begin(), sorted.end() ); cout << sorted << '\t' << pairing << '\n'; } } // aaaaaaabkllms alabamaalaska // aaaaaabilmnorz alabamaarizona // aaaaaaabklmnrss alabamaarkansas // aaaaaiklnorsz alaskaarizona // aaaaaakklnrsss alaskaarkansas // aaaaaiknnorrssz arizonaarkansas
1. Declare an array to hold the combination strings, and a second array to hold the sorted strings.
2. Iterate through all possible combinations of two states.
2-1. Sort the letters of each combination.
2-2. Store the unsorted string in the first array, and the sorted string in the second.
3. Iterate through all elements in the second array.
3-1. Compare each sorted string to every other sorted string.
3-2. If the sorted strings match, then report the unsorted strings.
int main() { vector<string> pairing(1225), sorted(1225); // Load all two-state combinations in an array for (int i=0, m=0; i < 49; ++i) for (int j=i+1; j < 50; ++j, ++m) { sorted[m] = pairing[m] = states[i] + states[j]; // Sort each combination and store it in a different array sort( sorted[m].begin(), sorted[m].end() ); } // Take each entry in the sorted array for (int i=0; i < sorted.size()-1; ++i) // ... and compare it to every other entry in the sorted array for (int j=i+1; j < sorted.size(); ++j) if (sorted[i] == sorted[j]) cout << pairing[i] << " ... " << pairing[j] << '\n'; } To see the answer, select/highlight from here to END northcarolinasouthdakota ... northdakotasouthcarolina END
is a more inspired approach possible?
What about sorting the sorted combinations, and then looking for 2 adjacent combinations that are equal?We will need to keep the sorted string and the unsorted string "together" so that when the sorted strings match, then the unsorted strings can be reported.
Is there a C++ data structure with sufficient mojo for this context?
multimap
?
It maintains its contents in sorted order.
It supports duplicate entries (in fact - these duplicates are exactly what we are looking for).
Check out the following example, and then use this new tool to refactor the previous solution.
int main() { multimap<string,string> mapping; string pairing, sorted; for (int i=0; i < 49; ++i) for (int j=i+1; j < 50; ++j) { sorted = pairing = states[i] + states[j]; sort( sorted.begin(), sorted.end() ); mapping.insert( make_pair( sorted, pairing ) ); } multimap::iterator iter = mapping.begin(); for (int i=0; i < 15; ++i, ++iter) cout << iter->first << '\t' << iter->second << '\n'; } // aaaaaaabkllms alabamaalaska // aaaaaaabklmnrss alabamaarkansas // aaaaaabbeklmnrs alabamanebraska // aaaaaabcehlmmssssttu alabamamassachusetts // aaaaaabcfiillmnor alabamacalifornia // aaaaaabchillmnnoorrt alabamanorthcarolina // aaaaaabchillmnoorstu alabamasouthcarolina // aaaaaabdeellmrw alabamadelaware // aaaaaabdelmnv alabamanevada // aaaaaabdhklmnoortt alabamanorthdakota // aaaaaabdhklmoosttu alabamasouthdakota
1. Iterate through all possible combinations of two states.
1-1. Sort the letters of each combination.
1-2. "Pair-up" each sorted representation with its unsorted original.
1-3. Insert each pairing into a
multimap
object.2. After the
multimap
is loaded, declare 2 iterator objects on themultimap
, and advance one of the iterators one step.3. Increment both iterator objects until 2 of the sorted strings are found to be equal.
int main() { multimap<string,string> mapping; string pairing, sorted; // Load all sorted pairings into a self-sorting multimap for (int i=0; i < 49; ++i) for (int j=i+1; j < 50; ++j) { sorted = pairing = states[i] + states[j]; sort( sorted.begin(), sorted.end() ); // all insertions are maintained in alphabetical order mapping.insert( make_pair( sorted, pairing ) ); } // Find adjacent pairings that are equal multimap<string,string>::iterator it1 = mapping.begin(), it2 = mapping.begin(); for (++it2; it2 != mapping.end(); ++it1, ++it2) if (it1->first == it2->first) cout << it1->second << " ... " << it2->second << '\n'; }
multimap
,multimap
?
Is it possible to merge the 2 sets into a single set?
1. Iterate through all possible combinations of two states.
1-1. Sort the letters of each combination.
1-2. Before each sorted string is loaded into a
map
data structure, check to see if that string already exists.1-2-1. If the string exists, then report the answer.
1-2-2. If the string does not exist, then add it to the
map
.
int main() { map<string,string> mapping; string pairing, sorted; for (int i=0; i < 49; ++i) for (int j=i+1; j < 50; ++j) { sorted = pairing = states[i] + states[j]; sort( sorted.begin(), sorted.end() ); // If this sorted pairing has not already been loaded, then load it if (mapping[sorted].empty()) mapping[sorted] = pairing; // Otherwise, report the match else cout << pairing << " ... " << mapping[sorted] << '\n'; } }