Broken keyboard (Text Tragedy) Java UVa11988

Broken keyboard (Text Tragedy) Java UVa11988

One algorithm or work, this is actually a acm title. Online search a bit, numbered UVa11988 . Ado, directly on the question.

topic

Do you have a broken keyboard. All the keys on the keyboard are working properly, but sometimes the Home button or press the End key automatically. You do not know the existence of this problem keyboard, but rather to concentrate on playing the manuscript, did not even turn on the monitor. When you turn on the monitor to show in front of you it was a tragedy text. Your task is to calculate the text of this tragedy before opening the display.

Comprising a plurality of sets of input data. Each set of data per line, comprising no more than 100,000 letters, the underscore character "[" or "]." In which the character "[" indicates the Home key, "]" represents the End key. Enter the ending mark for the end of file (EOF). Enter the file does not exceed 5MB. For each test, an output line, i.e. tragic text on the screen.

Input:
This_is_a_ [Beiju] _TEXT

[[]][][]Happy_Birthday_to_Tsinghua_University

Output:
BeijuThis_is_a_text

Happy_Birthday_to_Tsinghua_University

analysis

In because the Home and End keys, and the cursor is constantly on the move, where to move from that we will continue to write characters. In the data structure is presented in the insert.

Array inconvenient to make insertion operation, the mobile elements each time consuming. The list suitable for insertion, so they use a linked list structure (for not explain why the list here; in addition, whether the use is really a linked list or data can be combined list ideas, here is my list)

Head and tail pointers and arrays incoming talk method, met '[' or ']', it is moved directly to the head or REAR flag, and then continue.

Otherwise, it is judged flag at the tail node or in an intermediate / the first node , the node at the end on the normal add, but also remember flag and the rear pointer to the last element; in intermediate / the first node to be introduced when a tmp, add the new node to do, do normal insertion operation, then the flag point tmp.

Java code

import java.io.BufferedInputStream;
import java.util.Scanner;

/**
 * @ClassName BrokenKeyboard
 * @Description 悲剧的文本
 * @author 滑技工厂 https://blog.csdn.net/qq_41718454
 * @date 2020/3/7
 * @version 1.0
 */
public class BrokenKeyboard {

    public static void main(String[] args) {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        String s = sc.nextLine();
        char[] chars = s.toCharArray();
        Node node = new Node();
        Node rear = node;
        node.create(node,rear,chars);
        node.print(node);
    }


    static class Node {
        char data;
        Node next;
        public Node() {
        }

        public Node(char data, Node next) {
            this.data = data;
            this.next = next;
        }
        public void print(Node node) {

            while (node != null) {
                if (node.data != 0)
                    System.out.print(node.data);
                node = node.next;
            }

        }
        /*
         * @Title create
         * @Description create方法,遇见'['从头指针插入,遇见']'从尾指针插入
         * @author 滑技工厂
         * @Date 2020/3/7
         * @param [head, a]
         * @return void
         * @throws
         */
        public void create(Node head, Node rear, char a[]) {
            //指针开始等于头结点
            Node flag = head;
            for (int i = 0; i < a.length; i++) {

                if (a[i] == '[') {
                    //[ 指针移到头结点
                    flag = head;
                    continue;
                } else if (a[i] == ']') {
                    //指针移到尾结点
                    flag = rear;
                    continue;
                }

                if (flag.next == null) {//flag=rear 在尾结点
                    flag.next = new Node(a[i], null);
                    flag = flag.next;
                    //保证rear始终在尾结点
                    rear = flag;
                } else {//不在尾结点,头结点还是中间结点都一样,next有值

                    Node tmp = new Node(a[i], flag.next);
                    flag.next = tmp;
                    flag = tmp;
                }

            }
        }

    }

}

There is help for you if you point a little praise it, what advice can comment.

Finally mascot Town House
Here Insert Picture Description

Published 41 original articles · won praise 94 · views 9559

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104710877