Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Modified XML code Problem 2 (1 / 2)
- It is given a modified XML code. The modified XML code uses the symbols "["and"]" which are the opening and closing tag, respectively, instead of the standard symbols "<" and ">". Check if all the tags in the code are properly nested (code is valid), i.e. does every opening tag have suitable closing tag with the same name at the appropriate place in the code. For simplification, it is given that every opening tag must have a closing tag and tags do not have attributes.
- The number of code lines is given in the first input line. Then each XML tag is written in a new line. The output should print 1 for valid or 0 for invalid code.
- Explanation: In the modified XML code, every opening tag is structured as [nameOfTag], and every closing tag as [/ nameOfTag].
- Example for correct nested tags in XML:
- [tag1]
- [tag2]
- data
- [/tag2]
- [/tag1]
- Example for incorrect nested tags in XML:
- [tag1]
- [tag2]
- Podatok
- [/tag1]
- [/tag2]
- Име на класата (Java): CheckXML
- */
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.NoSuchElementException;
- interface Stack<E> {
- // The elements of the Stack are any kind of objects
- // Access methods:
- public boolean isEmpty ();
- // Returns true only if the stack is empty.
- public E peek ();
- // Returns the element on the top od the stack.
- // Transformation methods:
- public void clear ();
- // Clears the stack.
- public void push (E x);
- // Adds x on the top of the stack.
- public E pop ();
- // Removes and returns the element on the top.
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack (int maxDepth) {
- // Creating new empty stack
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty () {
- // Returns true only if the stack is empty.
- return (depth == 0);
- }
- public E peek () {
- // Returns the element on the top od the stack.
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth-1];
- }
- public void clear () {
- // Clears the stack.
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push (E x) {
- // Adds x on the top of the stack.
- elems[depth++] = x;
- }
- public E pop () {
- // Removes and returns the element on the top.
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class CheckXML {
- public static int check( String redovi[],ArrayStack<String> tagStack)
- {
- for(int i=0;i<redovi.length;i++)
- {
- if(redovi[i].charAt(0)=='[')
- {
- if(redovi[i].charAt(1)!='/')
- tagStack.push(redovi[i].substring(1));
- else
- {
- if(tagStack.isEmpty())
- return 0;
- if(tagStack.pop().equals(redovi[i].substring(2))==false)
- return 0;
- }
- }
- }
- if(!tagStack.isEmpty())
- return 0;
- return 1;
- }
- public static void main(String[] args) throws Exception{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = br.readLine();
- int n = Integer.parseInt(s);
- String [] redovi = new String[n];
- for(int i=0;i<n;i++)
- redovi[i] = br.readLine();
- int valid;
- ArrayStack<String> tagStack = new ArrayStack<String>(1000);
- valid = check(redovi, tagStack);
- System.out.println(valid);
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment