From b38d9929f7a015b56b847fde7e83f814f354497e Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 11 Nov 2019 17:55:52 +0700 Subject: One does not simply do CP well in NNN --- cpptour/Vector-test.cc | 9 ++++++ cpptour/Vector.cc | 23 +++++++++++--- cpptour/Vector.h | 3 ++ cpptour/weirdo.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 cpptour/weirdo.cc (limited to 'cpptour') diff --git a/cpptour/Vector-test.cc b/cpptour/Vector-test.cc index b49ff85..fbdf129 100644 --- a/cpptour/Vector-test.cc +++ b/cpptour/Vector-test.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -13,8 +14,16 @@ neg_length () catch (bad_alloc) { cout << "BIG OOF!" << endl; } } +void +init () +{ + Vector v {7.4, 3.2, 5.2, 6.9, 9.5, 4.2, 21.7}; + assert(v[5] == 4.2); +} + int main () { neg_length (); + init (); } diff --git a/cpptour/Vector.cc b/cpptour/Vector.cc index aa09eef..8f94345 100644 --- a/cpptour/Vector.cc +++ b/cpptour/Vector.cc @@ -12,14 +12,27 @@ Vector::Vector (int s) sz = s; } -double& Vector::operator[] (int i) +Vector::Vector (initializer_list lst) +: elem {new double[lst.size()]}, sz {static_cast (lst.size())} { - if (i < 0 || size() <= i) - throw out_of_range{"Vector::operator[]"}; - return elem[i]; + copy(lst.begin(), lst.end(), elem); +} + +Vector::~Vector () +{ + delete[] elem; } -int Vector::size () noexcept +int +Vector::size () noexcept { return sz; } + +double& +Vector::operator[] (int i) +{ + if (i < 0 || size() <= i) + throw out_of_range{"Vector::operator[]"}; + return elem[i]; +} diff --git a/cpptour/Vector.h b/cpptour/Vector.h index c7bbc68..8508503 100644 --- a/cpptour/Vector.h +++ b/cpptour/Vector.h @@ -1,8 +1,11 @@ class Vector { public: Vector (int s); + Vector (std::initializer_list); + ~Vector (); double& operator[] (int i); int size () noexcept; + void push_back (double); private: double* elem; int sz; diff --git a/cpptour/weirdo.cc b/cpptour/weirdo.cc new file mode 100644 index 0000000..f79336c --- /dev/null +++ b/cpptour/weirdo.cc @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef unordered_map charmap; +typedef set charset; + +constexpr double INF = 1e7; +const charset VOWELS {'a', 'e', 'i', 'o', 'u'}; + +inline size_t +sqr (size_t i) +{ + return i * i; +} + +bool +isvowel (const string& s, size_t i) +{ + try { return VOWELS.count (s.at (i)); } + catch (out_of_range const& e) { return true; } +} + +void +update (const string& s, charmap& x, charmap& f) +{ + charset b; + for (const auto& c : s) + { + f[c]++; + b.insert (c); + } + for (const auto& c : b) + x[c]++; +} + +int +main () +{ + size_t t, l; + string s; + + cin >> t; + while (t--) + { + charmap xa, fa, xb, fb; + cin >> l; + while (l--) + { + cin >> s; + size_t i = s.size (); + bool a = true; + + while (i--) + if (isvowel (s, i - 1) + isvowel (s, i) + isvowel (s, i + 1) < 2) + { + update (s, xb, fb); + a = false; + break; + } + if (a) + update (s, xa, fa); + } + + double sc = 1.0; + for (const auto& p : xa) + sc *= p.second; + for (const auto& p : fa) + sc /= sqr (p.second); + for (const auto& p : xb) + sc /= p.second; + for (const auto& p : fb) + sc *= sqr (p.second); + if (sc > INF) + cout << "Infinity" << endl; + else + cout << sc << endl; + } +} -- cgit 1.4.1