State name puzzle: Difference between revisions

Content added Content deleted
(→‎{{header|C++}}: cleaning up)
Line 265:
=={{header|C++}}==
Ported from C solution.
<lang C++cpp>#include <algorithm>
#include <stdio.hiostream>
#include <cstdlib>
#include <string>
#include <cstdlibarray>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
template<typename T>
// some common code
T unique(T&& src)
template<class C_, class LT_> C_ Unique(const C_& src, const LT_& less)
{
C_ T retval(std::move(src));
std::sort(retval.begin(), retval.end(), std::less<typename T::value_type>());
retval.erase(std::unique(retval.begin(), retval.end()), retval.end());
return retval;
}
template<class C_> C_ Unique(const C_& src)
{
return Unique(src, std::less<C_::value_type>());
}
 
#define USE_FAKES 1
 
vector<string>auto states = Uniqueunique(std::vector<std::string>({
#if USE_FAKES
"Slender Dragon", "Abalamara",
#endif
"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", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas",
"Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"
}));
 
struct CountedPair_counted_pair
{
std::string name_name;
std::array<int, 26> count{};
vector<unsigned char> count_;
 
void Addcount_characters(const std::string& s)
{
{
for (auto&& c : s) {
if (c >= 'Aa' && c <= 'Zz') ++count_count[c - 'Aa']++;
{
if (c >= 'aA' && c <= 'zZ') ++count_count[c - 'aA']++;
}
if (c >= 'A' && c <= 'Z') ++count_[c - 'A'];
}
}
}
 
CountedPair_ counted_pair(const std::string& s1, const std::string& s2) : name_(s1 + " + " + s2), count_(26, 0u)
: name(s1 + " + " + s2)
{
{
Add(s1); Add(s2);
count_characters(s1);
}
count_characters(s2);
}
};
 
bool operator<(const CountedPair_counted_pair& lhs, const CountedPair_counted_pair& rhs)
{
const int s1 = lhs.name_.size(),auto s2lhs_size = rhslhs.name_name.size();
auto rhs_size = rhs.name.size();
return s1 == s2
return lhs_size == rhs_size
? lexicographical_compare(lhs.count_.begin(), lhs.count_.end(), rhs.count_.begin(), rhs.count_.end())
? std::lexicographical_compare(lhs.count.begin(),
: s1 < s2;
lhs.count.end(),
rhs.count.begin(),
rhs.count.end())
: lhs_size < rhs_size;
}
 
bool operator==(const CountedPair_counted_pair& lhs, const CountedPair_counted_pair& rhs)
{
return lhs.name_name.size() == rhs.name_name.size() && lhs.count == rhs.count;
&& lhs.count_ == rhs.count_;
}
 
int main(void)
void FindPairs()
{
const int n_states = states.size();
 
std::vector<CountedPair_counted_pair> pairs;
for (int i = 0; i < n_states; i++) {
for (int j = 0; j < i; j++) {
pairs.emplace_back(counted_pair(states[i], states[j]));
}
sort(pairs.begin(), pairs.end());
}
std::sort(pairs.begin(), pairs.end());
 
auto start = pairs.begin();
while (true) {
for (;;)
auto match = std::adjacent_find(start, pairs.end());
{
auto match = adjacent_find if (start,match == pairs.end()); {
break;
if (match == pairs.end())
}
break;
auto next = match + 1;
std::cout << match->name_name << " => " << next->name_name << "\n";
start = next;
}
}
}</lang>
}
 
int main(void)
{
FindPairs();
return 0;
}
</lang>
 
=={{header|D}}==