public
anonymous / gist:4695755
Created

Beautiful strings.

  • Download Gist
gistfile1.cpp
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/*
CC0 1.0 Universal (CC0 1.0)
Public Domain
https://creativecommons.org/publicdomain/zero/1.0/
*/
 
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdexcept>
#include <algorithm>
#include <array>
#include <cctype>
 
 
struct beautiful_string
{
beautiful_string (std::string const& s);
unsigned maximum_possible_beauty() const;
std::string value_;
};
 
 
beautiful_string::beautiful_string
(std::string const& s): value_ (s)
{
if ((s.length() < 2) || (s.length() > 500)) {
throw std::range_error ("Beauty string length out of range 2 <= l <= 500");
}
}
 
 
struct down_counter
{
down_counter(unsigned const u): u_ (u) {}
unsigned operator*() const { return u_; }
down_counter& operator++() { --u_; return *this; }
unsigned u_;
};
 
 
unsigned
beautiful_string::maximum_possible_beauty
() const
{
std::array<unsigned, 26> beauty_table;
beauty_table.fill(0);
 
std::for_each (value_.begin(), value_.end(),
[&beauty_table](char c) -> void {
if (std::isalpha(c)) {
c |= char(1 << 5);
c -= 97;
++beauty_table[c];
}
});
 
std::sort (beauty_table.begin(), beauty_table.end(), std::greater<char>());
return std::inner_product (beauty_table.begin(), beauty_table.end(),
down_counter(beauty_table.size()), 0);
}
 
 
std::vector<beautiful_string>
get_m_lines_of_beautiful_strings
(std::istream& is, unsigned long m)
{
std::vector<beautiful_string> beautiful_strings;
 
if ((m < 5) || (m > 50))
throw std::range_error ("Number of beautiful strings out of range 5 <= m <= 50");
 
beautiful_strings.reserve (m);
auto bs_inserter = std::back_inserter (beautiful_strings);
 
std::generate_n (bs_inserter, m,
[&is]() -> std::string {
std::string s;
std::getline(is, s);
return s;
});
 
return beautiful_strings;
}
 
 
int main
(int const argc, char* argv[])
{
std::ifstream src_file;
unsigned long m = 0;
 
src_file.exceptions (std::ifstream::failbit | std::ifstream::badbit);
 
if (argc < 2)
return EXIT_FAILURE;
 
try {
std::string m_str;
 
src_file.open (argv[1]);
std::getline (src_file, m_str);
m = std::stoul (m_str);
 
std::vector<beautiful_string> beautiful_strings = get_m_lines_of_beautiful_strings (src_file, m);
 
unsigned int i = 1;
std::for_each (beautiful_strings.begin(), beautiful_strings.end(),
[&i](beautiful_string const& bs) -> void
{ std::cout << "Case #" << i++ << ": " << bs.maximum_possible_beauty() << std::endl; });
 
return EXIT_SUCCESS;
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
 
return EXIT_FAILURE;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.