Skip to content

Instantly share code, notes, and snippets.

@sguzman
Last active August 29, 2015 14:12
Show Gist options
  • Save sguzman/a441d8b2b904dbef909b to your computer and use it in GitHub Desktop.
Save sguzman/a441d8b2b904dbef909b to your computer and use it in GitHub Desktop.
Custom reader for input
#include <iostream>
#include <cstdlib>
namespace {
static inline void init(void) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
}
constexpr static const size_t BUFF{256};
struct cats {
static inline bool containsCats(char* ptr) {
for (; *ptr != EOF ; ++ptr) {
if (matchCats(ptr)) {
return true;
}
}
return false;
}
static inline bool matchCats(char* ptr) {
return Node1(ptr);
}
static inline bool Node1(char* ptr) {
constexpr static const char match{'C'};
if (ptr[0] == match) {
return Node2(++ptr);
}
return rejectState();
}
static inline bool Node2(char* ptr) {
constexpr static const char match{'a'};
if (ptr[0] == match) {
return Node3(++ptr);
}
return rejectState();
}
static inline bool Node3(char* ptr) {
constexpr static const char match{'t'};
if (ptr[0] == match) {
return Node4(++ptr);
}
return rejectState();
}
static inline bool Node4(char* ptr) {
constexpr static const char match1{'s'};
constexpr static const char match2{'t'};
if (ptr[0] == match1) {
return Node5(++ptr);
} else if (ptr[0] == match2) {
return Node6(++ptr);
}
return acceptState();
}
static inline bool Node5(char* ptr) {
return acceptState();
}
static inline bool Node6(char* ptr) {
constexpr static const char match{'y'};
if (ptr[0] == match) {
return Node7(++ptr);
}
return rejectState();
}
static inline bool Node7(char* ptr) {
return acceptState();
}
constexpr static inline bool acceptState(void) {
return true;
}
constexpr static inline bool rejectState(void) {
return false;
}
};
class Input final {
class statics final {
public:
template <typename... A>
statics(A... a) = delete;
inline ~statics(void)
{}
static inline size_t abstractRead(std::istream& in, char* buffer) {
in.getline(buffer, BUFF - 1);
return size_t(in.gcount() - 1);
}
static inline size_t forLoop(std::istream& in, char* buff, void(body)(char*)) {
size_t lines{0};
for (auto size = abstractRead(in, buff); in.good(); size = abstractRead(in, buff)) {
++lines;
buff[size] = EOF;
body(buff);
}
return lines;
}
};
class privates final {
public:
template <typename... A>
privates(A... a) = delete;
static inline size_t abstractRead(Input& super) {
return statics::abstractRead(super.in, super.buffer);
}
static inline size_t forLoop(Input& super, void (body) (char*)) {
size_t lines{0};
for (auto size = abstractRead(super); super.in.good(); size = abstractRead(super)) {
++lines;
super.buffer[size] = EOF;
body(super.buffer);
}
return lines;
}
};
inline size_t forLoop(void (body) (char*)) {
return privates::forLoop(*this, body);
}
public:
explicit inline Input(std::istream& input) : in(input)
{}
explicit inline Input(std::istream&& input) : Input(input)
{}
template <typename... A>
explicit Input(A... a) noexcept = delete;
inline void iterate(void (body) (char*)) {
forLoop(body);
}
inline bool done(void) {
return in.eof();
}
private:
std::istream& in;
char buffer[256];
Input* self;
};
}
int main(void) {
init();
Input reader{std::cin};
reader.iterate([] (char* ptr) {
if (cats::containsCats(ptr)) {
std::cout << "Yes!" << std::endl;
} else {
std::cout << "No!" << std::endl;
}
});
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment