Блог пользователя aslf010990

Автор aslf010990, 10 лет назад, По-английски

Hello everyone, I am trying to solve this problem, I've tried 4 times and am getting wrong answer , I ask you to please guide me a little to solve the probelema , this is my code , greetings and thanks .

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static boolean ok,ok2;
    public static class Node
    {
        public char c;
        public Node next,ant;
        public Node(char c)
        {
            this.c=c;
            this.next=null;
        }
    }
    public static class Lista
    {
        public Node first,last;
        public Lista()  //constructor
        {
            this.first=this.last=null;
        }
        public Lista InsertBegin(Node nuevo)
        {
            nuevo.next=first;
            first=nuevo;
            if(!ok)
                last=first;
            ok=true;
            return this;
        }
        public Lista InsertEnd(Node nuevo)
        {
            if(!ok2)
            {
                nuevo.next=first;
                first=nuevo;
                last=first;
                ok=true;
            }
            else
            {
                last.next=nuevo;
                last=nuevo;
            }
            ok2=true;
            return this;
        }
        public Lista Visualize()
        {
            for(Node i=first;i!=null;i=i.next)
                out.write(i.c+"");
            out.write("\n");
            return this;
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
        String s="";
        while((s=cin.readLine())!=null)
        {
            char arre[]=s.toCharArray();
            Lista lis=new Lista();
            ok=ok2=false;
            int t=arre.length,aux=0;
            for(int i=0;i<t;i++)
            {
                if(arre[i]=='[')
                {
                    aux=i+1;
                    while(aux<t && arre[aux]!='[' && arre[aux]!=']')
                        aux++;
                    for(int j=aux-1;j>i;j--)
                        lis.InsertBegin(new Node(arre[j]));
                    i=aux-1;
                }
                else if(arre[i]!=']')
                    lis.InsertEnd(new Node(arre[i]));
            }
            lis.Visualize();
        }
        out.flush();
    }
}
  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится