Question:
Simulate a text editor. You can enter letters, numbers, and underscores (_). If '[' is used, it is considered as the Home key. If it is set to ']', it is used as the end key.
Ask what string is displayed on the final screen.
Analysis:
It is time-consuming to move a large number of characters in an array. Therefore, the next array indicates the character number on the Right of S [I] in the display. The variable cur simulates the cursor, that is, the current cursor is located on the right of S [cur.
Variable last record the subscript of the last character in the display.
The connection I understand is like this:
1 // # define local 2 # include <cstdio> 3 # include <cstring> 4 5 const int maxn = 100000 + 10; 6 int last, cur, next [maxn]; 7 char s [maxn]; 8 9 int main (void) 10 {11 # ifdef local12 freopen ("11988in.txt", "r", stdin ); 13 # endif14 15 while (scanf ("% s", S + 1) = 1) 16 {17 int n = strlen (S + 1 ); 18 last = cur = 0; 19 next [0] = 0; 20 21 for (INT I = 1; I <= N; ++ I) 22 {23 char CH = s [I]; 24 if (s [I] = '[') cur = 0 ; 25 else if (s [I] = ']') cur = last; 26 else27 {28 next [I] = next [cur]; 29 next [cur] = I; 30 if (cur = last) Last = I; // update the last character number 31 cur = I; // move the cursor 32} 33} 34 35 for (INT I = next [0]; I! = 0; I = next [I]) 36 printf ("% C", s [I]); 37 puts (""); 38} 39 40 return 0; 41}
Code Jun
Ultraviolet A 11988 (array simulated linked list) Broken keyboard (a.k. A. beiju text)