Algorithm


Problem Name: beecrowd | 3140

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3140

Copying and Pasting Code

 

By Roger Eliodoro Condras, UFSC-ARA BR Brazil

Timelimit: 1

During the pandemic, with the temporary suspension of the academic calendar at UFSC, Lucas has taken advantage of his free time to take several online courses and learn to use new technologies and programming libraries.

He recently participated in a free bootcamp on the use of Node.js and the ReactJS library and fell in love. He liked it so much that he decided to port a website that he had created in HTML for this new format.

Luckily for Lucas, he can reuse most of the HTML scripts, but some parts are no longer necessary, since Node.js and + ReactJS start generating them automatically. As there are several files for him to analyze and give control + c, control + v in the codes, he would like your help to speed up the process.

Given an HTML file, you must write a program that returns only the content between the "<body>" and "</body>" tags, everything else must be ignored.

Since Lucas is a capricious guy, the code is properly indented. In the opening and closing lines of the body tag there is nothing but the tag itself and indent spaces.

 

Input

 

The entry has several lines, the lines of the HTML file provided by Lucas, and ends with EOF. Each line consists of a sequence of printable characters from the ASCII Table and it is guaranteed that none of them is longer than 1,000 characters or is blank.

The body opening and closing tags, "<body>" and "</body>" respectively, are guaranteed to appear only once in the entire input file.

 

Output

 

You must print all the lines between the "<body>" and "</body>" tags without including them and keeping the exact original formatting of the file.

 

 

 

Input Sample Output Sample

 

<html>
    <head>
        <title>Meu primeiro programa em HTML</title>
    </head>
    <body>
         <h1>Hello World!</h1>
    </body>
</html>

 

 

         <h1>Hello World!</h1>	

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <string.h>
#include <stdbool.h>
#include <stdio.h>

int main()
{

    char string[1100];

    bool body = false;
    while (~scanf("%[^\n]%*c", string))
    {

        if (strstr(string, " < body>"))
        {

            body = true;
            continue;
        }

        if (body && strstr(string, " < /body>"))
            body = false;

        if (body)
            puts(string);
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Meu primeiro programa em HTML

Hello World!

Output

x
+
cmd

Hello World!

#2 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);

    string a;
    vector < string> ans;

    //Indicará se ocorreu uma tag de abertura .
    bool flag = false;
    while(getline(cin, a)){
        string x  = "";
        for(int i = 0; i  <  a.size(); ++i){
            if(a[i] != ' ' && a[i] != '\n' && a[i] != '\0')
                x += a[i];
        }   
        if(flag && x != " < /body>")
            ans.emplace_back(a);
        else{
            if(x == "")
                flag = true;
            else if(x == " < /body>")
                flag = false;
        }
    }

    for(auto c:ans)
        cout << c << '\n';

    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
Meu primeiro programa em HTML

Hello World!

Output

x
+
cmd

Hello World!

#3 Code Example with Javascript Programming

Code - Javascript Programming


const { createReadStream } = require("node:fs")
const { createInterface } = require("node:readline")

//// READING FILE | STREAMS ////
class LineReader {
	/**
	 * @param {import("node:fs").PathLike} path
	 * @param {BufferEncoding} encoding
	 * @return {import("node:readline").ReadLine}
	 */
	static createReadLineInterface(path, encoding = "utf8") {
		const readStreamOptions = {
			encoding: encoding,
			flags: "r",
			emitClose: true,
			autoClose: true
		}

		return createInterface({
			input: createReadStream(path, readStreamOptions),
			crlfDelay: Infinity,
			terminal: false
		})
	}

	/**
	 * @param {import("node:fs").PathLike} path
	 * @param {BufferEncoding} encoding
	 */
	static create(path, encoding) {
		const RLI = LineReader.createReadLineInterface(path, encoding)

		let EOF = false

		const nextLineGenerator = (async function* () {
			for await (const line of RLI) {
				yield line
			}
		})()

		RLI.once("close", () => {
			EOF = true
		})

		return {
			hasNextLine: () => !EOF,
			nextLine: async (/** @type {unknown} */ fn) => {
				const { value } = (await nextLineGenerator.next())
				return (typeof fn === "function") ? fn(value) : value
			},
			close: () => RLI.close()
		}
	}
}

//// MAIN ////
async function main() {
	const PATH = "/dev/stdin"
	const ENCODING = "ascii"
	const RLI = LineReader.createReadLineInterface(PATH, ENCODING)

	let innerHTMLBody = false

	RLI.on("line", (line) => {
		if (line.includes("")) innerHTMLBody = true
		if (line.includes("")) innerHTMLBody = false

		if (innerHTMLBody === true && line.includes("") === false)
			console.log(line)
	})
}

main()

Copy The Code & Try With Live Editor

Input

x
+
cmd
Meu primeiro programa em HTML

Hello World!

Output

x
+
cmd

Hello World!

#4 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto){ return lines.shift(); };

var exit = false
while(true){
    var x = prompt("")
    if(x.match("")){
        while(true){
            var y = prompt("")
            if(y.match("")){
                exit = true
                break
            }
            console.log(y)
        }
    }
    if(exit){
        break
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Meu primeiro programa em HTML

Hello World!

Output

x
+
cmd

Hello World!

Advertisements

Demonstration


Previous
#3091 Beecrowd Online Judge Solution 3091 Rest 1.0 Solution in C, C++, Java, Js and Python
Next
#3142 Beecrowd Online Judge Solution 3142 Excel Bug Solution in C, C++, Java, Js and Python