Connection Status:
Competition Arena > bloggoShortcuts
SRM 214 · 2004-10-06 · by Eeyore · String Manipulation
Class Name: bloggoShortcuts
Return Type: String
Method Name: expand
Arg Types: (string)
Problem Statement

Problem Statement

You are helping to develop a weblog-management system called bloggo. Although bloggo pushes all content to the front end of a website in HTML, not all content authors enjoy using HTML tags in their text. To make their lives easier, bloggo offers a simple syntax called shortcuts to achieve some HTML textual effects. Your job is to take a document written with shortcuts and translate it into proper HTML.

One shortcut is used to make italicized text. HTML does this with the <i> and </i> tags, but in bloggo, an author can simply enclose a piece of text using two instances of the underscore character, '_'. Thus, where a content author writes


  You _should_ see the baby elephant at the zoo!

bloggo will publish the following instead.


  You <i>should</i> see the baby elephant at the zoo!

Another shortcut serves to render text in boldface, which HTML accomplishes with <b> and </b> tags. Bloggo lets content authors do the same with paired instances of the asterisk character, '*'. When a content author writes the text


  Move it from *Receiving* to *Accounts Payable*.

it will end up on the website as


  Move it from <b>Receiving</b> to <b>Accounts Payable</b>.

Given a String, text, containing zero or more usages of the italic and boldface shortcuts, translate it into HTML as demonstrated by the examples above. There will be an even number of underscores and of asterisks in text, respectively, and the spans of text enclosed by successive pairs of these characters will not overlap. To render a span of text in italics in HTML, you must start with the <i> tag and end with the </i> tag. For boldface text, start with <b> and end with </b>. After rendering all shortcuts in HTML, return the resulting text as a String.

Constraints

  • text is between 1 and 50 characters long, inclusive
  • the only characters allowed in text are the alphabetic characters 'a' to 'z' and 'A' to 'Z', the underscore '_', the asterisk '*', the space character, and the punctuation symbols ',', ';', '.', '!', '?', '-', '(', and ')'.
  • the underscore '_' occurs in text an even number of times
  • the asterisk '*' occurs in text an even number of times
  • no substring of text enclosed by a balanced pair of underscores or by a balanced pair of asterisks may contain any further underscores or asterisks
Examples
0)
"You _should_ see the new walrus at the zoo!"
Returns: "You should see the new walrus at the zoo!"

A walrus is a large, blubbery cousin of the seal.

1)
"Move it from *Accounts Payable* to *Receiving*."
Returns: "Move it from Accounts Payable to Receiving."

Notice that a boldface span may enclose several words.

2)
"I saw _Chelydra serpentina_ in *Centennial Park*."
Returns: "I saw Chelydra serpentina in Centennial Park."

One piece of text may include italics as well as boldface.

3)
" _ _ __  _ yabba dabba _   *  dooooo  * ****"
Returns: "      yabba dabba      dooooo   "

Shortcuts may enclose spaces or nothing at all.

4)
"_now_I_know_*my*_ABC_next_time_*sing*it_with_me"
Returns: "nowIknowmyABCnexttimesingitwithme"

Submissions are judged against all 61 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class bloggoShortcuts with a public method string expand(string text) · 61 test cases · 2 s / 256 MB per case

Submitting as anonymous