One problem: Beautiful String
This is the 2014 Microsoft School Recruit programming problem, test instructions roughly as follows:
If a string consists of three or more groups of consecutive ascending letters, each set of equal lengths, then we call this string beautiful
- Examples of compliant beautiful string: ABC, CDE, AABBCC, AAABBBCCC
- Inconsistent Beautiful string Example: Abd,cba,aabbc,zab
Enter a string containing only lowercase letters, if it contains a substring of beautiful, output yes, otherwise output no
- Input: The first line is the number of cases, after each line is a number, a string, the number indicates the length of the string, the length is less than 10MB
- Output: YES or NO
Finite state machine FSM definition
Finite state machines (finite-state machine), also known as finite state automata, are mathematical models of behaviors such as the finite state and the transfer and movement between these states. Commonly used with: Regular expression engine, compiler lexical and syntactic analysis, game design, network protocol, enterprise application medium.
Finite state machine FSM features
State machine can be summed up as 4 elements, namely, present state, condition, action, and sub-state. This induction is mainly due to the internal causal relationship of the state machine to consider.
"Present state" and "condition" are the result of, "action" and "sub-state" is the fruit.
1. Present state: Refers to the current status.
2. Condition: Also known as "event", when a condition is satisfied, an action is triggered, or a state migration is performed.
3. Action: Action performed after the condition is met. After the action is completed, you can migrate to a new state, or you can still remain in the original state. The action is not required and can be migrated directly to the new state 4 when the condition is satisfied or no action is taken. The new state to be moved to after the condition is satisfied. "Secondary state" is relative to the "present state", the "secondary state" once activated, it becomes a new "present state".
FSM implementation of finite state machine
- With switch/case or if/else realization, simple rough, suitable for simple small state machine;
- Using the state pattern in the design pattern, the logic of complex judgment is simplified, which facilitates the organization of code;
- Using the state table design, establish the state table and the action query table, according to the state table, the event, the action table to locate the corresponding action processing function, the execution completes after the state switch;
The solution analysis of Beautiful string problem
This machine is small in size, directly with if/else simple implementation can. Each shape consists of 4 elements:
- Currently processed characters current
- The number of characters currently processed num_current
- Beautiful the number of previous characters in a string Num_prev
- The current element is the beautiful string in the first few elements pos_beauty
Code
#include <iostream>using namespace std;struct states {char current; The character being processed int num_prev; Beautiful the number of the previous element in string int num_current; The cumulative number of characters being processed int pos_beauty; The character being processed is the first element of the beautiful string} s = {0, 0, 0, 0};int main () {int ncase, N;char c;cin >> ncase;while (ncase-- {cin >> N;bool result = False;while (n--) {cin >> c;if (result) {continue;} if (s.current = = 0) {s.current = C;s.num_current = 1;s.pos_beauty = 1;continue;} if (s.current = = c) {s.num_current++;if (S.num_prev! = 0 && s.num_current > S.num_prev) {s.num_prev = 0;s.pos_b Eauty = 1;}} if (s.current = = c-1) {if (S.num_prev = = 0 | | s.num_current <= s.num_prev) {s.pos_beauty++;} else {s.pos_beauty = 2;} S.num_prev = S.num_current;s.num_current = 1;} if (s.current! = c && s.current! = c-1) {s.pos_beauty = 1;s.num_current = 0;s.num_current = 1;} if (s.pos_beauty >= 3 && s.num_current = = S.num_prev) {result = true;} S.current = C;} if (result) {CoUT << "YES" << Endl;} else {cout << "NO" << Endl;}} return 0;} /** use case: 73ABC4AAAB6ABCCDE3ABB8AAAABBCC11AAAABBBCCDE6AAABBC */
Algorithm learning Note (ix) Application of finite state machine FSM