You can divide those words into some basic words like
out,in,put,on,e.
Then build a gragh which has 5 points expressing those 5 words mentioned above.
f[i,j,k]means from i to j state[i]=k(k=1 means i must be a start,k=-1 means i must be a end,k=0 means both are ok)state[j]=f[i,j,k]);
Read every basic words and DFS the gragh.
The details are left for you:)