fork(9) download
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <complex>
  4. #include <cstring>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. template <class T> struct is_iterator {
  12. template <class U, typename enable_if<!is_convertible<U, const char*>::value, int>::type = 0>
  13. constexpr static auto has_indirection(int) -> decltype(*declval<U>(), bool()) { return true; }
  14. template <class> constexpr static bool has_indirection(long) { return false; }
  15. constexpr static bool value = has_indirection<T>(0);
  16. };
  17.  
  18. using uint = unsigned int;
  19. // Buffer size should be 2^12 or 2^13 for optimal performance with files.
  20. const uint BUFFER_SIZE = 1 << 12;
  21. // Maximum possible length of a string representing primitive type
  22. // assuming we won't encounter huge double values.
  23. const uint MAX_LENGTH = 1 << 7;
  24.  
  25. namespace Detail {
  26. struct Width { uint value; };
  27. struct Fill { char value; };
  28. struct Base { uint value; };
  29. struct Precision { uint value; };
  30. struct Delimiter { const char* value; };
  31. } // namespace Detail
  32.  
  33. Detail::Width setWidth(uint value = 0) { return {value}; }
  34. Detail::Fill setFill(char value = ' ') { return {value}; }
  35. Detail::Base setBase(uint value = 10) { assert(2 <= value && value <= 36); return {value}; }
  36. Detail::Precision setPrecision(uint value = 9) { assert(value < MAX_LENGTH); return {value}; }
  37. Detail::Delimiter setDelimiter(const char* value = " ") { return {value}; }
  38.  
  39. /******************************* input classes ********************************/
  40. class InputDevice {
  41. protected:
  42. const char* head;
  43. const char* tail;
  44.  
  45. InputDevice(const char* head, const char* tail) : head(head), tail(tail), base(setBase().value) {}
  46.  
  47. virtual void fillInput() = 0;
  48.  
  49. inline char nextChar() {
  50. if (__builtin_expect(head >= tail, false)) fillInput();
  51. return *head++;
  52. }
  53.  
  54. template <class I> int readUnsignedIntGeneral(I& arg, char c) {
  55. I value = 0;
  56. int length = 0;
  57. for (;; ++length, c = nextChar()) {
  58. if (isDigit(c)) c -= '0';
  59. else if (isUpper(c)) c -= 'A' - 10;
  60. else if (isLower(c)) c -= 'a' - 10;
  61. else c = base;
  62. if (c >= base) break;
  63. value = base * value + c;
  64. }
  65. arg = value;
  66. return --head, length;
  67. }
  68.  
  69. template <class I> inline int readUnsignedInt(I& arg, char c) {
  70. if (__builtin_expect(base > 10, false)) return readUnsignedIntGeneral(arg, c);
  71. I value = 0;
  72. int length = 0;
  73. for (; static_cast<unsigned char>(c - '0') < base; ++length, c = nextChar())
  74. value = base * value + c - '0';
  75. arg = value;
  76. return --head, length;
  77. }
  78.  
  79. template <class I> inline bool readSignedInt(I& arg, char c) {
  80. bool negative = c == '-';
  81. if (negative) c = nextChar();
  82. typename make_unsigned<I>::type unsignedArg;
  83. if (readUnsignedInt(unsignedArg, c) == 0) return false;
  84. arg = negative ? ~static_cast<I>(unsignedArg - 1) : static_cast<I>(unsignedArg);
  85. return true;
  86. }
  87.  
  88. template <class F> bool readFloatingPoint(F& arg, char c) {
  89. bool negative = c == '-';
  90. if (negative) c = nextChar();
  91. unsigned long long integerPart;
  92. if (readUnsignedInt(integerPart, c) == 0) return false;
  93. arg = static_cast<F>(integerPart);
  94. if (nextChar() == '.') {
  95. unsigned long long fractionalPart = 0;
  96. int fractionalLength = readUnsignedInt(fractionalPart, nextChar());
  97. if (fractionalLength > 0) {
  98. unsigned long long basePower = 1;
  99. for (; fractionalLength; --fractionalLength) basePower *= base;
  100. arg += static_cast<F>(fractionalPart) / basePower;
  101. }
  102. } else --head;
  103. if (negative) arg = -arg;
  104. return true;
  105. }
  106.  
  107. public:
  108. uint base;
  109.  
  110. InputDevice(InputDevice const&) = delete;
  111. InputDevice& operator = (InputDevice const&) = delete;
  112.  
  113. static inline bool isSpace(char c) { return static_cast<unsigned char>(c - '\t') < 5 || c == ' '; }
  114. static inline bool isDigit(char c) { return static_cast<unsigned char>(c - '0') < 10; }
  115. static inline bool isUpper(char c) { return static_cast<unsigned char>(c - 'A') < 26; }
  116. static inline bool isLower(char c) { return static_cast<unsigned char>(c - 'a') < 26; }
  117. static inline bool isOneOf(char c, const char* str) { return strchr(str, c) != nullptr; }
  118.  
  119. void putBack() { --head; } // can be called only once directly after successfully reading a character
  120.  
  121. inline bool readChar(char& arg) {
  122. if (__builtin_expect(head >= tail, false)) {
  123. fillInput();
  124. if (__builtin_expect(head >= tail, false)) return arg = '\0', false;
  125. }
  126. return arg = *head++, true;
  127. }
  128.  
  129. template <class UnaryPredicate>
  130. inline char skipCharacters(UnaryPredicate isSkipped) {
  131. char c;
  132. do { c = nextChar(); } while (isSkipped(c));
  133. return c;
  134. }
  135. inline char skipCharacters() { return skipCharacters(isSpace); }
  136.  
  137. template <class UnaryPredicate>
  138. inline int readString(char* arg, int limit, UnaryPredicate isTerminator) {
  139. skipCharacters(isTerminator);
  140. // put back first non-skipped character, reserve space for null character
  141. int charsRead = 0;
  142. for (--head, --limit; head < tail; fillInput()) {
  143. ptrdiff_t chunkSize = find_if(head, min(tail, head + limit - charsRead), isTerminator) - head;
  144. arg = copy_n(head, chunkSize, arg);
  145. head += chunkSize;
  146. charsRead += chunkSize;
  147. if (chunkSize == 0 || head < tail) break;
  148. }
  149. return *arg = '\0', charsRead;
  150. }
  151.  
  152. inline int readString(char* arg, int limit, const char* terminators) {
  153. if (!*terminators) return readString(arg, limit, InputDevice::isSpace);
  154. return readString(arg, limit, [terminators](char c) { return InputDevice::isOneOf(c, terminators); });
  155. }
  156.  
  157. // property setters
  158. inline bool read(Detail::Base newBase) { base = newBase.value; return true; }
  159. // primitive types
  160. inline bool read() { return true; }
  161. inline bool read(char& arg) { return readChar(arg); }
  162. template <class I> inline typename enable_if<is_integral<I>::value && is_unsigned<I>::value,
  163. bool>::type read(I& arg) { return readUnsignedInt(arg, skipCharacters()) > 0; }
  164. template <class I> inline typename enable_if<is_integral<I>::value && is_signed<I>::value,
  165. bool>::type read(I& arg) { return readSignedInt(arg, skipCharacters()); }
  166. template <class F> inline typename enable_if<is_floating_point<F>::value,
  167. bool>::type read(F& arg) { return readFloatingPoint(arg, skipCharacters()); }
  168. // characters skip
  169. inline bool read(const char& arg) { skipCharacters([arg](char c) { return arg != c; }); return true; }
  170. inline bool read(const char* arg) {
  171. if (*arg) skipCharacters([arg](char c) { return InputDevice::isOneOf(c, arg); });
  172. else skipCharacters();
  173. return putBack(), true;
  174. }
  175. inline bool read(bool (*isSkipped)(char)) { skipCharacters(isSkipped); putBack(); return true; }
  176. // strings
  177. template <class I, class Terminator, class... Ts> inline typename enable_if<is_integral<I>::value,
  178. bool>::type read(char* arg, I limit, Terminator terminator, Ts&&... args) {
  179. readString(arg, static_cast<int>(limit), terminator);
  180. return read(forward<Ts>(args)...);
  181. }
  182. template <class I> inline typename enable_if<is_integral<I>::value,
  183. bool>::type read(char* arg, I limit) { return read(arg, limit, ""); }
  184. template <class... Ts>
  185. inline bool read(char* first, char* last, Ts&&... args) {
  186. return read(first, static_cast<int>(last - first), forward<Ts>(args)...);
  187. }
  188. template <int N, class... Ts>
  189. inline bool read(char (&arg)[N], Ts&&... args) { return read(static_cast<char*>(arg), N, forward<Ts>(args)...); }
  190. template <class Terminator, class... Ts>
  191. inline bool read(string& arg, Terminator terminator, Ts&&... args) {
  192. for (int length = 16, last = 0;; last += length, length <<= 1) {
  193. arg.resize(last + length);
  194. int charsRead = readString(&arg[last], length + 1, terminator);
  195. if (charsRead < length) {
  196. arg.resize(last + charsRead);
  197. return read(forward<Ts>(args)...);
  198. }
  199. }
  200. }
  201. inline bool read(string& arg) { return read(arg, ""); }
  202. // complex types and ranges
  203. template <class T1, class T2>
  204. inline bool read(pair<T1, T2>& arg) { return read(arg.first, arg.second); }
  205. template <class T>
  206. inline bool read(complex<T>& arg) {
  207. T real, imag;
  208. if (!read(real, imag)) return false;
  209. arg.real(real), arg.imag(imag);
  210. return true;
  211. }
  212. template <class T>
  213. inline bool read(vector<T>& arg) {
  214. uint n;
  215. if (!read(n)) return false;
  216. arg.resize(n);
  217. return read(arg.begin(), arg.end());
  218. }
  219. template <class Iterator, class... Ts> inline typename enable_if<is_iterator<Iterator>::value,
  220. bool>::type read(Iterator first, Iterator last, Ts&&... args) {
  221. for (; first != last; ++first) if (!read(*first)) return false;
  222. return read(forward<Ts>(args)...);
  223. }
  224. template <class Iterator, class I, class... Ts>
  225. inline typename enable_if<is_iterator<Iterator>::value && is_integral<I>::value,
  226. bool>::type read(Iterator first, I count, Ts&&... args) { return read(first, first + count, forward<Ts>(args)...); }
  227. // generic forwarding
  228. template <class T>
  229. inline auto read(T& arg) -> decltype(arg.read(*this)) { return arg.read(*this); }
  230. template <class T0, class T1, class... Ts>
  231. inline typename enable_if<!is_iterator<T0>::value && !is_convertible<T0, char*>::value,
  232. bool>::type read(T0&& arg0, T1&& arg1, Ts&&... args) {
  233. return read(forward<T0>(arg0)) && read(forward<T1>(arg1), forward<Ts>(args)...);
  234. }
  235. };
  236.  
  237. class InputFile : public InputDevice {
  238. FILE* file;
  239. bool lineBuffered;
  240. bool owner;
  241. char buffer[BUFFER_SIZE];
  242.  
  243. void fillInput() override {
  244. head = buffer;
  245. *buffer = '\0';
  246. if (__builtin_expect(!lineBuffered, true)) {
  247. tail = head + fread(buffer, 1, BUFFER_SIZE, file);
  248. } else {
  249. tail = head;
  250. if (fgets(buffer, BUFFER_SIZE, file)) while (*tail) ++tail;
  251. }
  252. }
  253.  
  254. public:
  255. InputFile(FILE* file = stdin, bool lineBuffered = true, bool takeOwnership = false)
  256. : InputDevice(buffer, buffer) , file(file), lineBuffered(lineBuffered), owner(takeOwnership) {}
  257. InputFile(const char* fileName) : InputFile(fopen(fileName, "r"), false, true) {}
  258. ~InputFile() { if (owner) fclose(file); }
  259. };
  260.  
  261. // Picks up data appended to the string but doesn't handle reallocation.
  262. class InputString : public InputDevice {
  263. void fillInput() override { while (*tail) ++tail; }
  264.  
  265. public:
  266. InputString(const string& s) : InputDevice(s.data(), s.data() + s.size()) {}
  267. InputString(const char* s) : InputDevice(s, s + strlen(s)) {}
  268. };
  269.  
  270. /******************************* output classes *******************************/
  271. class OutputDevice {
  272. protected:
  273. char buffer[BUFFER_SIZE + MAX_LENGTH];
  274. char* output;
  275. char* end;
  276. bool separate;
  277.  
  278. OutputDevice() : output(buffer), end(buffer + BUFFER_SIZE + MAX_LENGTH), separate(false)
  279. , width(setWidth().value), fill(setFill().value), base(setBase().value), precision(setPrecision().value)
  280. , delimiter(setDelimiter().value) { computeBasePower(); }
  281.  
  282. virtual void writeToDevice(uint count) = 0;
  283.  
  284. inline void flushMaybe() {
  285. if (__builtin_expect(output >= buffer + BUFFER_SIZE, false)) {
  286. writeToDevice(BUFFER_SIZE);
  287. output = copy(buffer + BUFFER_SIZE, output, buffer);
  288. }
  289. }
  290.  
  291. void computeBasePower() {
  292. basePower = 1;
  293. for (uint i = 0; i < precision; ++i) basePower *= base;
  294. }
  295.  
  296. template <class I> inline char* writeUnsignedInt(I arg, char* last) {
  297. if (__builtin_expect(arg == 0, false)) *--last = '0';
  298. if (__builtin_expect(base == 10, true)) {
  299. for (; arg; arg /= 10) *--last = '0' + arg % 10;
  300. } else for (; arg; arg /= base) {
  301. I digit = arg % base;
  302. *--last = digit < 10 ? '0' + digit : 'A' - 10 + digit;
  303. }
  304. return last;
  305. }
  306.  
  307. template <class I> inline char* writeSignedInt(I arg, char* last) {
  308. auto unsignedArg = static_cast<typename make_unsigned<I>::type>(arg);
  309. if (arg < 0) {
  310. last = writeUnsignedInt(~unsignedArg + 1, last);
  311. *--last = '-';
  312. return last;
  313. }
  314. return writeUnsignedInt(unsignedArg, last);
  315. }
  316.  
  317. template <class F> char* writeFloatingPoint(F arg, char* last) {
  318. bool negative = signbit(arg);
  319. if (negative) arg = -arg;
  320. if (isnan(arg)) for (int i = 0; i < 3; ++i) *--last = i["NaN"];
  321. else if (isinf(arg)) for (int i = 0; i < 3; ++i) *--last = i["fnI"];
  322. else {
  323. auto integerPart = static_cast<unsigned long long>(arg);
  324. auto fractionalPart = static_cast<unsigned long long>((arg - integerPart) * basePower + F(0.5));
  325. if (fractionalPart >= basePower) ++integerPart, fractionalPart = 0;
  326. char* point = last - precision;
  327. if (precision > 0) {
  328. ::fill(point, writeUnsignedInt(fractionalPart, last), '0');
  329. *--point = '.';
  330. }
  331. last = writeUnsignedInt(integerPart, point);
  332. }
  333. if (negative) *--last = '-';
  334. return last;
  335. }
  336.  
  337. inline int writeT(char* first) {
  338. int delimiterLenght = separate ? writeDelimiter() : 0;
  339. separate = true;
  340. uint charsWritten = static_cast<uint>(end - first);
  341. if (__builtin_expect(charsWritten < width, false))
  342. charsWritten += writeFill(width - charsWritten);
  343. output = copy(first, end, output);
  344. flushMaybe();
  345. return delimiterLenght + static_cast<int>(charsWritten);
  346. }
  347.  
  348. inline int writeFill(uint count) {
  349. int charsWritten = static_cast<int>(count);
  350. if (__builtin_expect(output + count + MAX_LENGTH < end, true)) {
  351. if (count == 1) *output++ = fill;
  352. else output = fill_n(output, count, fill);
  353. } else for (uint chunkSize = static_cast<uint>(buffer + BUFFER_SIZE - output);; chunkSize = BUFFER_SIZE) {
  354. if (chunkSize > count) chunkSize = count;
  355. output = fill_n(output, chunkSize, fill);
  356. flushMaybe();
  357. if ((count -= chunkSize) == 0) break;
  358. }
  359. return charsWritten;
  360. }
  361.  
  362. public:
  363. uint width;
  364. char fill;
  365. uint base;
  366. uint precision;
  367. unsigned long long basePower;
  368. string delimiter;
  369.  
  370. OutputDevice(OutputDevice const&) = delete;
  371. OutputDevice& operator = (OutputDevice const&) = delete;
  372. virtual ~OutputDevice() {};
  373.  
  374. inline int writeChar(char arg) { separate = false; *output++ = arg; flushMaybe(); return 1; }
  375.  
  376. inline int writeString(const char* arg, size_t length, bool checkWidth = true) {
  377. separate = false;
  378. uint count = static_cast<uint>(length);
  379. int charsWritten = static_cast<int>(count) + (checkWidth && count < width ? writeFill(width - count) : 0);
  380. if (__builtin_expect(output + count + MAX_LENGTH < end, true)) {
  381. if (count == 1) *output++ = *arg;
  382. else output = copy_n(arg, count, output);
  383. } else for (uint chunkSize = static_cast<uint>(buffer + BUFFER_SIZE - output);; chunkSize = BUFFER_SIZE) {
  384. if (chunkSize > count) chunkSize = count;
  385. output = copy_n(arg, chunkSize, output);
  386. flushMaybe();
  387. if ((count -= chunkSize) == 0) break;
  388. arg += chunkSize;
  389. }
  390. return charsWritten;
  391. }
  392.  
  393. inline int writeDelimiter() { return writeString(delimiter.c_str(), delimiter.size(), false); }
  394.  
  395. inline void flush() {
  396. writeToDevice(static_cast<uint>(output - buffer));
  397. output = buffer;
  398. }
  399.  
  400. // property setters
  401. inline int write(Detail::Width newWidth) { width = newWidth.value; return 0; }
  402. inline int write(Detail::Fill newFill) { fill = newFill.value; return 0; }
  403. inline int write(Detail::Base newBase) { base = newBase.value; computeBasePower(); return 0; }
  404. inline int write(Detail::Precision newPrecision) {
  405. precision = newPrecision.value; computeBasePower(); return 0;
  406. }
  407. inline int write(Detail::Delimiter newDelimiter) { delimiter = newDelimiter.value; return 0; }
  408. // primitive types
  409. inline int write() { return 0; }
  410. inline int write(char arg) { return writeChar(arg); }
  411. template <class I> inline typename enable_if<is_integral<I>::value && is_unsigned<I>::value,
  412. int>::type write(I arg) { return writeT(writeUnsignedInt(arg, end)); }
  413. template <class I> inline typename enable_if<is_integral<I>::value && is_signed<I>::value,
  414. int>::type write(I arg) { return writeT(writeSignedInt(arg, end)); }
  415. template <class F> inline typename enable_if<is_floating_point<F>::value,
  416. int>::type write(F arg) { return writeT(writeFloatingPoint(arg, end)); }
  417. // complex types
  418. inline int write(const char* arg) { return writeString(arg, strlen(arg)); }
  419. template <int N>
  420. inline int write(char (&arg)[N]) { return writeString(arg, strlen(arg)); }
  421. inline int write(const string& arg) { return writeString(arg.c_str(), arg.size()); }
  422. template <class T1, class T2>
  423. inline int write(const pair<T1, T2>& arg) {
  424. int charsWritten = write(arg.first);
  425. charsWritten += writeDelimiter();
  426. return charsWritten + write(arg.second);
  427. }
  428. template <class T>
  429. inline int write(const complex<T>& arg) { return write(real(arg), imag(arg)); }
  430. // ranges
  431. template <class Iterator, class... Ts> inline typename enable_if<is_iterator<Iterator>::value,
  432. int>::type write(Iterator first, Iterator last, Ts&&... args) {
  433. int charsWritten = 0;
  434. for (; first != last; charsWritten += ++first == last ? 0 : writeDelimiter()) charsWritten += write(*first);
  435. return charsWritten + write(forward<Ts>(args)...);
  436. }
  437. template <class Iterator, class I, class... Ts>
  438. inline typename enable_if<is_iterator<Iterator>::value && is_integral<I>::value,
  439. int>::type write(Iterator first, I count, Ts&&... args) { return write(first, first + count, forward<Ts>(args)...); }
  440. // generic forwarding
  441. template <class T>
  442. inline auto write(const T& arg) -> decltype(arg.write(*this)) { return arg.write(*this); }
  443. template <class T0, class T1, class... Ts> inline typename enable_if<!is_iterator<T0>::value,
  444. int>::type write(T0&& arg0, T1&& arg1, Ts&&... args) {
  445. int charsWritten = write(forward<T0>(arg0));
  446. return charsWritten + write(forward<T1>(arg1), forward<Ts>(args)...);
  447. }
  448. };
  449.  
  450. class OutputFile : public OutputDevice {
  451. FILE* file;
  452. bool owner;
  453.  
  454. void writeToDevice(uint count) override {
  455. fwrite(buffer, 1, count, file);
  456. fflush(file);
  457. }
  458.  
  459. public:
  460. OutputFile(FILE* file = stdout, bool takeOwnership = false) : file(file), owner(takeOwnership) {}
  461. OutputFile(const char* fileName) : OutputFile(fopen(fileName, "w"), true) {}
  462. ~OutputFile() override { flush(); if (owner) fclose(file); }
  463. };
  464.  
  465. class OutputString : public OutputDevice {
  466. string& str;
  467.  
  468. void writeToDevice(uint count) override { str.append(buffer, count); }
  469.  
  470. public:
  471. OutputString(string& str) : OutputDevice(), str(str) {}
  472. ~OutputString() override { flush(); }
  473. };
  474.  
  475. unique_ptr<InputDevice> input;
  476. unique_ptr<OutputDevice> output;
  477.  
  478. template <class... Ts> inline bool read(Ts&&... args) { return input->read(forward<Ts>(args)...); }
  479. template <class... Ts> inline int write(Ts&&... args) { return output->write(forward<Ts>(args)...); }
  480. template <class... Ts> inline int writeln(Ts&&... args) { return write(forward<Ts>(args)..., '\n'); }
  481. void flush() { output->flush(); }
  482.  
  483. /*******************************************************************************
  484.  * Read returns true if all the arguments were successfully read. Parameters:
  485.  * - setBase(uint): base for integer and floating point numbers
  486.  * Single variable of one of the following types:
  487.  * - char, standard integer and floating point types
  488.  * - pair, complex
  489.  * - vector (size and then the elements)
  490.  * Characters skip:
  491.  * - char: skip until the given character is encountered and read it
  492.  * - const char*: skip all the characters from the string
  493.  * - predicate: skip all the characters satisfying the predicate
  494.  * Strings: read until character limit is reached or termination character is found
  495.  * (one of the characters in a given string or defined by predicate, isspace by default)
  496.  * - char (&)[N], terminator
  497.  * - char*, int limit, terminator
  498.  * - string&, terminator
  499.  * Ranges:
  500.  * - Iterator first, Iterator last
  501.  * - Iterator first, int count
  502.  *******************************************************************************
  503.  * Write returns number of characters written. Parameters:
  504.  * - setWidth(uint): minimum width of a single element to write (except character)
  505.  * - setFill(char): character prepended to an element until set width is reached
  506.  * - setBase(uint): base for integer and floating point numbers
  507.  * - setPrecision(uint): number of digits after the decimal point
  508.  * - setDelimiter(const char*): delimiter automatically inserted between elements
  509.  * that are not strings or characters
  510.  * Single variable of one of the following types:
  511.  * - char, standard integer and floating point types
  512.  * - string, const char*
  513.  * - pair, complex
  514.  * Ranges:
  515.  * - Iterator first, Iterator last
  516.  * - Iterator first, int count
  517.  ******************************************************************************/
  518.  
  519. const int N = 1001;
  520. int n, m;
  521. char s[N][N];
  522. int a[N], b[N];
  523.  
  524. int main() {
  525. input.reset(new InputFile(stdin, false));
  526. output.reset(new OutputFile());
  527.  
  528. read(n, m, s, n, a, n, b, m);
  529. writeln(n, m, '\n', setDelimiter("\n"), s, n);
  530. writeln(setDelimiter(", "), a, n, '\n', b, m);
  531. return 0;
  532. }
  533.  
Success #stdin #stdout 0s 4468KB
stdin
3 4
qwer
asdf
zxcv
1 2 3
1 2 3 4
stdout
3 4
qwer
asdf
zxcv
1, 2, 3
1, 2, 3, 4