Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <random>
- #include <string>
- #include <string_view>
- #include <execution>
- #include "log_duration.h"
- using namespace std;
- string GenerateQuery(mt19937& generator, int max_length, int space_rate) {
- const int length = uniform_int_distribution(0, max_length)(generator);
- string query(length, ' ');
- for (char& c : query) {
- const int rnd = uniform_int_distribution(0, space_rate - 1)(generator);
- if (rnd > 0) {
- c = 'a' + (rnd - 1);
- }
- }
- return query;
- }
- template <typename Solver>
- void Test(string_view mark, string_view s, Solver solver) {
- int result;
- {
- LOG_DURATION(mark);
- result = solver(s);
- }
- cout << result << endl;
- }
- #define TEST(solver) Test(#solver, s, solver)
- int CountWords(std::string_view str) {
- std::size_t wc = (str.front() != ' ' ? 1 : 0);
- return wc += std::transform_reduce(
- execution::par,
- str.begin(),
- str.end()-1,
- str.begin()+1,
- 0,
- std::plus<>{}, [](char left, char right) {
- return left == ' ' && right != ' ';
- }
- );
- }
- int main() {
- // должно вывести 3
- cout << CountWords("pretty little octopus "sv) << endl;
- mt19937 generator;
- const string s = GenerateQuery(generator, 100'000'000, 4);
- TEST(CountWords);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement