Connection Status:
Competition Arena > Inserter
TCO '03 Qual. Round 1 · 2003-10-07 · by lars2520 · String Manipulation, String Parsing
Class Name: Inserter
Return Type: String
Method Name: insert
Arg Types: (vector<string>, string)
Problem Statement

Problem Statement

A common task in text editing is inserting one sequence of characters before another one. For example, in the String "10 10 bills", we might want to insert a dollar sign ('$') before the second occurrence of the number "10". Thus, the original string would become "10 $10 bills". You are to write a method which takes an original String and a series of insert instructions and performs the instructions, in the order they are given. You should return the modified String.

Instructions will be of the form "#<insert># #<before># <index>", where <insert> is the sequence of characters to be inserted, <before> is the sequence of characters before which to insert <insert>, and <index> is the index of the occurrence of <before> (starting from 1), before which to insert <insert>. In order to facilitate spaces in <insert> and <before>, both sequences will be surrounded by '#'. Thus the command "#insert this# #before this# 3" would insert "insert this" before the third occurrence of "before this".

If, for some command, there are less than <index> occurrences of <before>, then do nothing. For example, in the String "$100 $100", there is no third occurrence of "$", so a command that inserts before the third occurrence of "$" should be ignored.

Notes

  • All of the instructions are performed on the String as it changes.
  • Searching for occurrences of <before> should be case sensitive.
  • Note that the ith occurrence of <before> may overlap with the (i+1)st occurrence of <before>. See example 5.

Constraints

  • original and each element of commands will have between 1 and 50 characters, inclusive.
  • commands will contain between 0 and 50 elements, inclusive.
  • Each character in the input will have ASCII value between 32 and 126, inclusive.
  • Each element of commands will be formatted as described in the problem statement as "## ## ".
  • The character '#' will not exist within either or .
  • Both and will contain at least one character.
  • Each in command will be between 1 and 2^31-1, inclusive, with no extra leading zeros.
  • The return will have at most 999 characters.
Examples
0)
{"## #$# 2","#comes # #before this# 1"}
"$100 before this $300"
Returns: "$100 comes before this $300"

The first command tells us to insert the sequence "" before the second occurrence of "$". Doing this gives us: "$100 before this $300" The second command tells us to insert "comes " before "before this". Doing this gives us "$100 comes before this $300"

1)
{"#,# # and# 1","#,# # and# 2","#,# # and# 3"}
"lions and tigers and bears"
Returns: "lions, and tigers, and bears"

Each command says to insert "," before " and". The first command says to insert before the 1st occurrence, the 2nd command says to insert before the 2nd occurrence, and the 3rd command says to insert before the 3rd occurrence (which does not exist).

2)
{"#import java.util.*; # #public class # 1"}
"public class Inserter"
Returns: "import java.util.*; public class Inserter"
3)
{"# # # # 1","# # # # 2","# # # # 3","# # # # 4","# # # # 5","# # # # 6"}
"add lots of spaces"
Returns: "add       lots of spaces"
4)
{"# # # # 6","# # # # 5","# # # # 4","# # # # 3","# # # # 2","# # # # 1"}
"order matters quite a bit"
Returns: "order  matters  quite  a  bit"

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

Coding Area

Language: C++17 · define a public class Inserter with a public method string insert(vector<string> commands, string original) · 72 test cases · 2 s / 256 MB per case

Submitting as anonymous