Broken Keyboard (a.k.a. Beiju Text)
You ' re typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard are that sometimes the "home" key or the "end" key gets automatically pressed (internall Y).
You ' re not aware of this issue, since your ' re focusing on the text and do not even turn on the monitor! After your finished typing, you can see a text in the screen (if you turn on the monitor).
In Chinese, we can call it beiju. Your task is to find the Beiju text.
Input
There is several test cases. Each test case was a single line containing at least one and at the most 100,000 letters, underscores and special character S ' [' and '] '. ' [' means the ' Home ' key is pressed internally, and '] ' means the ' End ' key is pressed internally. The input is terminated by End-of-file (EOF). The size of input file does not exceed 5MB.
Output
For each case, print the Beiju text in the screen.
Sample Input
This_is_a_[beiju]_text
[[]][][]happy_birthday_to_tsinghua_university
Output for the Sample Input
Beijuthis_is_a__text
Happy_birthday_to_tsinghua_university
The main topic: When you enter the article, the Home button on the keyboard and the end key out of the problem, will not be timed to press.
Text that gives you a keystroke, where ' [' means the home key, '] ' represents the end key, and outputs the text of the tragedy.
Idea: Using a linked list to simulate, encounter the home key, will be inserted in the text behind the text to the front, encountered
End key to insert the last side of the text. But using the list will use pointers, the process is more cumbersome. Here with a
Next array analog Pointing, Next[i] indicates the word Poute to the right of S[i] in the current display. Then use a cur to represent the current
The position of the cursor, last indicates the record position of the final character, so that the end key can be found directly next to the cursor
Point to the character position.
Specific reference: Algorithmic Competition Primer Classic (second edition) p143~144
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;char s[101000];int Next[101000];int Main () {int cur,last;//cur is the cursor position, last is the final character of the display while (~SCANF ("%s", s+1)) { memset (next,0,sizeof (Next)); int len = strlen (s+1); Next[0] = 0; cur = last = 0; for (int i = 1; I <= len; i++) {if (s[i] = = ' [') cur = 0; else if (s[i] = = '] ') cur = last; else {//Simulate Insert list procedure Next[i] = next[cur];//I-character points to the cursor position next[cur] = i; The cursor is pointing to the next character if (cur = = last)//only after the cursor is in the current position of the final character or encountered] after the previous = i; Cur = i;//Move Cursor}} for (int i = next[0]; i = 0; i = next[i]) printf ("%c", S[i]); printf ("\ n"); Memset (s,0,sizeof (s)); } return 0;}
UVA11988 Broken Keyboard (a.k.a. Beiju Text) "Array emulation linked list"