Under construction

Still being built. Things may change

Ignore all my comments (Small)

Time limit5sMemory limit512 MB

Summary
Remove every nested /* */ comment block from the document with a single left to right scan and print the rest unchanged.
Level

Medium4 of 10

Topics
Stack, String, Simulation
Solved
No attempts yet

Problem

Igor writes his comments in C style /* ... */ blocks, and he wants that one comment format everywhere, in Python, in Haskell, in HTML or XML documents. What he needs for that is a comment pre-processor: a program that deletes every comment block, meaning a /*, then the comment text, then a */. The processed text then goes to whatever compiler or renderer it belongs to.

The pre-processor is not a plain search and replace.

Comment blocks nest the way brackets nest in most programming languages, so a comment can hold another comment. The block below has one outer comment with two comments inside it.

printf("Hello /* a comment /* a comment inside comment */ 
        inside /* another comment inside comment */ 
        string */ world");

After the pre-processing step it becomes:

printf("Hello  world");

A comment can appear anywhere in the text, including inside a string "/*...*/", inside a numeric constant 12/*...*/34, and inside a character escape \/*...*/n.

Stated formally:

text:
  text-piece
  text-piece remaining-text
text-piece:
  char-sequence-without-/*
  empty-string
remaining-text:
  comment-block text

comment-block:
  /* comment-content */
comment-content:
  comment-piece
  comment-piece remaining-comment
comment-piece:
  char-sequence-without-/*-or-*/
  empty-string
remaining-comment:
  comment-block comment-content

char:
  letters
  digits
  punctuations
  whitespaces

Given a text, delete every comment-block in it.

Input

The input is one text document that may contain comment blocks written with /* and */. The document is valid: it follows the text rule above, so every block that opens also closes. The input always ends with a newline.

Output

First print the line

Case #1:

Then print the document with every comment block deleted. Do not delete any space or blank line that sits outside a comment.

Limits

The document is smaller than 2 KB (2048 bytes).

It contains only these characters:

  • letters a-z, A-Z
  • digits 0-9
  • punctuation ~ ! @ # % ^ & * ( ) - + = : ; " ' < > , . ? | / \ { } [ ] _
  • whitespace: the space character and the newline character

Hint

The pre-processor deletes comments in a single left to right pass. A block that appears only because another block was deleted is not deleted again. For example,

//*no recursion*/* file header */

becomes

/* file header */

A * that already belongs to a /* or a */ cannot be reused by another /* or */. So /*/ is not a complete comment block: the leading /* opens a comment and the remaining / becomes comment content.

Examples3

  1. Example 1

    Input
    //*no recursion*/* file header
    ***********/************
    * Sample input program *
    **********/*************
    */
    int spawn_workers(int worker_count) {
      /* The block below is supposed to spawn 100 workers.
         But it creates many more.
         Commented until I figure out why.
      for (int i = 0; i < worker_count; ++i) {
        if(!fork()) {
          /* This is the worker. Start working. */
          do_work();
        }
      }
      */
      return 0; /* successfully spawned 100 workers */
    }
    
    int main() {
      printf("Hello /*a comment inside string*/ world");
      int worker_count = 0/*octal number*/144;
      if (spawn_workers(worker_count) != 0) {
        exit(-1);
      }
      return 0;
    }
    
    Expected output
    Case #1:
    /* file header
    ************************
    */
    int spawn_workers(int worker_count) {
      
      return 0; 
    }
    
    int main() {
      printf("Hello  world");
      int worker_count = 0144;
      if (spawn_workers(worker_count) != 0) {
        exit(-1);
      }
      return 0;
    }
    
  2. Example 2

    Input
    //*no recursion*/* file header */
    
    Expected output
    Case #1:
    /* file header */
    
  3. Example 3

    Input
    a/*/b*/c
    
    Expected output
    Case #1:
    ac