Problem 271 -- Simply Syntax
Tips
Method One: Recursion.
Write two recursive functions. One test for whether a string is a "correct sentence". The other test for whether a string composes of two consecutive "correct sentences". These two functions shall call themselves and/or call each other.
The main function shall call these two functions as it is parsing the input string.
Method Two: Stack.
Read chars from input string and push them onto stack. Test the codition of the stack. If suitable, clear the stack. Return suitable signal of the correctness of the string.
Method Three: Simplified stack.
I discover this method when I was trying to solve the problem by a stack. I find that the content of the stack is not really important. What is important is the position of the stack pointer. By recording analysing the pointer it is possible to avoid the pushing and poping operations and get the work done.
Input
Cp Isz NIsz Cqpq C z zz Nz NNNNNz CNNNNNzNNNNNz CCppCpp CNCCpppCpp CCCpppp CCCppNpp
Output
NO YES YES NO NO YES NO YES YES YES YES YES YES YES