1. Title Description: Click to open the link
2. Problem-Solving ideas: The use of linked list to solve. Each character is entered, save it, set the input string is S[1...N], you can use Next[i] to indicate the character number on the right of S[i] in the current display. In order to facilitate subsequent processing, suppose that the front of S is also a virtual s[0] ( This is the list of common processing methods, it must be kept in mind), then next[0] represents the leftmost character in the display, and then a variable cur to indicate the position of the cursor: that is, the current cursor is located in S[cur] To the right (that is, our DOS window is often the next place to enter). Cur=0 indicates that the cursor is on the right side of S[0], which is the leftmost display. To move the cursor, you also need a variable last to indicate that the final character of the display is s[last].
Next consider how to use a linked list to achieve the subject. At first we needed a head node, and we might as well set it to 0, which is next[0]=0. The head node just acts as a marker and does not represent the real position. The head node in the linked list always points to the first element of the list. Then start inserting the element. inserting elements into a list is two lines of code: Next[i]=head;head=i. The first sentence indicates that the next element of the new element I points to the element that the current head points to, and the second sentence is to update the head point, pointing to the new first element I. Once all the elements have been inserted, just start from the beginning and follow the next array to output the entire list sequentially. Note that such inserts are actually inserted at the end of the chain, and the tail of the list is constantly getting longer. This problem uses cur as the head node, by moving the position of the head node can be implemented in different positions of the linked list to insert new elements. According to test instructions, when encountering ' [', the head node becomes 0 and encounters '] ', the head node becomes last. See the code comment for the detailed procedure.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 100000+5int last, cur, next[n]; Char S[n];int main () {//freopen ("T.txt", "R", stdin), while (~SCANF ("%s", S + 1)) {int N = strlen (s + 1); last = cur = 0; Next[0] = 0;for (int i = 1; I <= n; i++) {Char ch = s[i];if (ch = = ' [') cur = 0;//head node moved to position 0else if (ch = = '] ') cur = last;/ /head node moved to position lastelse//insert operation {Next[i] = Next[cur]; Next[cur] = i;if (cur = =) Last = I;cur = i;}} for (int i = next[0]; I! = 0; i = next[i])//Sequentially output list printf ("%c", S[i]); cout << Endl;} return 0;}
Example 6-4 broken keyboard UVa11988