Created
February 2, 2013 02:18
-
-
Save anonymous/4695755 to your computer and use it in GitHub Desktop.
Beautiful strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment