Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FSM
- {
- class Program
- {
- enum State
- {
- EMPTY, N, NA, NAN, NANO
- }
- static bool ContainsNano(string text)
- {
- State state = State.EMPTY;
- foreach (var sym in text)
- {
- switch (state)
- {
- case State.EMPTY:
- if (sym == 'n')
- {
- state = State.N;
- }
- break;
- case State.N:
- if (sym == 'a')
- {
- state = State.NA;
- }
- else if (sym == 'n')
- {
- // same state
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- case State.NA:
- if (sym == 'n')
- {
- state = State.NAN;
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- case State.NAN:
- if (sym == 'n')
- {
- state = State.N;
- }
- else if (sym == 'o')
- {
- return true;
- // state = State.NANO;
- }
- else if (sym == 'a')
- {
- state = State.NA;
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- }
- }
- return false;
- }
- static int CountNano(string text)
- {
- int cnt = 0;
- State state = State.EMPTY;
- foreach (var sym in text)
- {
- switch (state)
- {
- case State.EMPTY:
- if (sym == 'n')
- {
- state = State.N;
- }
- break;
- case State.N:
- if (sym == 'a')
- {
- state = State.NA;
- }
- else if (sym == 'n')
- {
- // same state
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- case State.NA:
- if (sym == 'n')
- {
- state = State.NAN;
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- case State.NAN:
- if (sym == 'n')
- {
- state = State.N;
- }
- else if (sym == 'o')
- {
- cnt++;
- state = State.NANO;
- }
- else if (sym == 'a')
- {
- state = State.NA;
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- case State.NANO:
- if (sym == 'n')
- {
- state = State.N;
- }
- else
- {
- state = State.EMPTY;
- }
- break;
- }
- }
- return cnt;
- }
- static void Main(string[] args)
- {
- Console.WriteLine(CountNano("nanonanonano"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement