fork download
  1. //! (c) WhiZTiM __ionogu(<_at_)>acm.org
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <string>
  7. #include <chrono>
  8. #include <tuple>
  9.  
  10. using IndexProd = std::tuple<std::size_t, long>;
  11.  
  12. //! Invariant: str.size() >= length
  13. IndexProd index_of_largest(const std::string& str, std::size_t length){
  14. long largest_product = 0;
  15. std::size_t largest_index = 0;
  16.  
  17. for(std::size_t i = 0; i < str.size() - length; i++){
  18. auto start = str.begin() + i;
  19. auto stop = start + length;
  20.  
  21. using Char = std::string::value_type;
  22. auto product = std::accumulate(start, stop, long(1),
  23. [](std::size_t sum, Char c){ return sum * (c - 48); });
  24. if(product > largest_product){
  25. largest_index = i;
  26. largest_product = product;
  27. }
  28. }
  29. return std::make_tuple(largest_index, largest_product);
  30. }
  31.  
  32. void print(const std::string& str, std::size_t index, std::size_t length){
  33. auto start = str.begin() + index;
  34. auto stop = start + length - 1;
  35. std::copy(start, stop, std::ostream_iterator<char>(std::cout, " x "));
  36. std::cout << *stop;
  37. }
  38.  
  39. std::string str_input();
  40.  
  41. void run(){
  42. std::ios::sync_with_stdio(false);
  43. const auto len = 13;
  44. std::string val;
  45. //std::cin >> val;
  46. val = str_input();
  47. auto index_prd = index_of_largest(val, len);
  48. std::cout << "Largest value at index: " << std::get<0>(index_prd) << '\n';
  49. std::cout << "Sequence is "; print(val, std::get<0>(index_prd), len);
  50. std::cout << " = " << std::get<1>(index_prd) << std::endl;
  51. }
  52.  
  53.  
  54. std::string str_input(){
  55. return "731671765313306249192251196744265747423553491949349698352031277450632623"
  56. "9578318016984801869478851843858615607891129494954595017379583319528532088055111"
  57. "2540698747158523863050715693290963295227443043557668966489504452445231617318564"
  58. "0309871112172238311362229893423380308135336276614282806444486645238749303589072"
  59. "9629049156044077239071381051585930796086670172427121883998797908792274921901699"
  60. "7208880937766572733300105336788122023542180975125454059475224352584907711670556"
  61. "0136048395864467063244157221553975369781797784617406495514929086256932197846862"
  62. "2482839722413756570560574902614079729686524145351004748216637048440319989000889"
  63. "5243450658541227588666881164271714799244429282308634656748139191231628245861786"
  64. "6458359124566529476545682848912883142607690042242190226710556263211111093705442"
  65. "1750694165896040807198403850962455444362981230987879927244284909188845801561660"
  66. "9791913387549920052406368991256071760605886116467109405077541002256983155200055"
  67. "93572972571636269561882670428252483600823257530420752963450";
  68. }
  69.  
  70.  
  71.  
  72. ////////////////////////////////////////////////// My timer function //////
  73. template<typename Func, typename... Args>
  74. void timer(Func func, Args&&... args){
  75. auto start = std::chrono::high_resolution_clock::now();
  76. func(std::forward<Args&&>(args)...);
  77. auto stop = std::chrono::high_resolution_clock::now();
  78. std::cout.precision(8);
  79. std::cout <<
  80. "Took: " << std::chrono::duration<double, std::ratio<1,1>>(stop - start).count()
  81. << " seconds\n";
  82. }
  83.  
  84. int main(){
  85. timer(run);
  86. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
Largest value at index: 503
Sequence is 9 x 7 x 8 x 1 x 7 x 9 x 7 x 7 x 8 x 4 x 6 x 1 x 7 = 2091059712
Took: 0.000199762 seconds