1 / 3
Apr 2022

Program works perfectly on my computer / ideone.
I do not use any generic collections, only system, implemented simple double linked list. I cannot get it within time though. Can you see where I can optimize this?
Note: output is a single string but it adds “\n” for each password.

using System;
namespace nabilhacker
{
    class Node
    {
        public Node prev;
        public Node next;
        public char ch;

        public Node(char ch)
        {
            this.ch = ch;
        }
    }
    class Program
    {

        static void Main()
        {
            Node head;
            Node current;
            int t = int.Parse(Console.ReadLine());
            string output = "";
            for (int j = 0; j < t; ++j)
            {
                string str = Console.ReadLine();
                head = null;
                current = null;
                foreach (char c in str)
                {
                    if (c != '<' && c != '>' && c != '-')
                    {
                        if (current == null)
                        {
                            current = new Node(c);
                            current.next = head;
                            if (head != null) head.prev = current;
                            head = current;
                        }
                        else
                        {
                            Node newNode = new Node(c);
                            newNode.next = current.next;
                            newNode.prev = current;
                            if (current.next != null) current.next.prev = newNode;
                            current.next = newNode;
                            current = newNode;
                        }
                    }
                    else if (c == '<')
                    {
                        if (current == null) continue;
                        current = current.prev;
                    }
                    else if (c == '>')
                    {
                        if (current == null) current = head;
                        else
                        if (current.next != null) current = current.next;
                    }
                    else
                    {
                        if (current == null) continue;
                        if (current.prev == null)
                        {
                            head = current.next;
                            current = null;
                            if (head != null) head.prev = null;
                        }
                        else
                        {
                            current.prev.next = current.next;
                            if (current.next != null) current.next.prev = current.prev;
                            current = current.prev;
                        }
                    }
                }
                while (head != null)
                {
                    output += head.ch;
                    head = head.next;
                }
                output += "\n";
            }
            Console.Write(output);
        }
    }
}
  • created

    Apr '22
  • last reply

    Apr '22
  • 2

    replies

  • 719

    views

  • 2

    users

  • 1

    link

from the docs3

How many times do you add to the output variable when the input can be a million characters? Try using an instance of StringBuilder instead.