Ultraviolet A 11988 (array simulated linked list) Broken keyboard (a.k. A. beiju text)

Source: Internet
Author: User
Tags define local

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.