#include <stdio.h>
enum states { before = 0, inside = 1, after = 2 };
class StateMachine
{
private:
enum states state; // Current state
struct branch
{
enum states new_state:2;
int should_putchar:1;
};
static struct branch transition_table[3][3]; // Transition map
public:
StateMachine() : state(before) {} // Constructor with starting state
void process_event(int c)
{
int idx2 = (c == ' ') ? 0 : (c == '\n') ? 1 : 2;
struct branch *b = & transition_table[state][idx2];
state = b->new_state;
if(b->should_putchar) putchar(c);
}
};
struct StateMachine::branch StateMachine::transition_table[3][3] =
{
/* ' ' '\n' others */
/* before */ { {before,0}, {before,1}, {inside,1} },
/* inside */ { {after, 0}, {before,1}, {inside,1} },
/* after */ { {after, 0}, {before,1}, {after, 0} }
};
int main(void)
{
int c;
StateMachine FSM;
while((c = getchar()) != EOF)
FSM.process_event(c);
return 0;
}