125B - Simple XML - CodeForces Solution


implementation *1000

Please click on ads to support us..

Python Code:

def solve():
    out = ""
    current_tag = ""
    tabs = 0
    for i in input():
        current_tag += i
        if i == ">":
            if current_tag[1] == "/":
                if tabs-1 < 0:
                    out += current_tag
                    out += '\n'
                    current_tag = ""
                else:
                    tabs -= 1
                    out += tabs*'  '
                    out += current_tag
                    out += '\n'
                    current_tag = ""
            else:
                out += tabs*'  '
                out += current_tag
                out += '\n'
                tabs += 1
                current_tag = ""

    print(out)


solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define rew(x)  for(int i = 0 ; i < x ; i++)

int main(){
    string s;
    cin >> s;
    int n = s.length();
    string space = "  ";
    int spc = 0;
    int looping = (n/7)*6;
    int temp = looping;
    int i = 0;
    while(looping){
        char a = s[i];
        
        


        if(a == '/'){
            looping += 1;
            spc -=1;
        }
        if(((a - 'a') <26) && ((a - 'a') >= 0)){
            if(s[i + 4] == a){
                spc -= 1;
            }
        }
        if(((a - 'a') <26) && ((a - 'a') >= 0)){
            if((s[i + 4] != a  ) && (s[i+3] == '/')){
                spc -= 1;
            }
        }



        cout << a;



        if(a == '>'){
            cout << "\n";
            spc+=1;
            rew(spc){
            cout << space;
        }
        }












        looping--;
        i++;
    }
    
}


Comments

Submit
0 Comments
More Questions

287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring