Connection Status:
Competition Arena > OperateString
SRM 779 · 2020-02-20 · by lazy_fox · Brute Force
Class Name: OperateString
Return Type: String
Method Name: operate
Arg Types: (string, vector<int>)
Problem Statement

Problem Statement

You have a string. Initially, this string is s.

You are going to perform a sequence of operations on this string. Each operation has an integer argument X. The operation is performed as follows:

  • If X is positive, X times take the first character of the string and move it to the end of the string.
  • If X is negative, -X times take the last character of the string and move it to the beginning of the string.
  • If X is zero, nothing happens.

You are given the initial string s and the int[] moves that describes a sequence of operations. Apply the operation with argument moves[0] to the initial string. Then, take the string you got and apply the operation with argument moves[1] to that string. And so on, until you have applied all the operations. Return the string you will have in the end.

Constraints

  • s will have between 1 and 1000 characters, inclusive.
  • Each character of s will be a lowercase English letter ('a'-'z').
  • moves will have between 1 and 100 elements, inclusive.
  • Each element of moves will be between -10^9 and 10^9, inclusive.
Examples
0)
"abcde"
{3}
Returns: "deabc"

When applying the operation, we move the 'a' to the end (producing "bcdea"), then we move the 'b' to the end (producing "cdeab"), and finally we move the 'c' to the end, producing the final output: "deabc".

1)
"abcde"
{1,2}
Returns: "deabc"

After the first operation (with argument 1) we have "bcdea", after the second operation (with argument 2) we have "deabc".

2)
"abracadabra"
{-3}
Returns: "braabracada"
3)
"topcoder"
{-5,7,-2,13}
Returns: "dertopco"

"topcoder" -> "codertop" -> "pcoderto" -> "topcoder" -> "dertopco".

4)
"havefunsolvingthissrmwatchoutfortimeoutsmakesureyoursolutionisfastenough"
{1000000000,-900000000,800000000,-700000000,600000000,543210987}
Returns: "ingthissrmwatchoutfortimeoutsmakesureyoursolutionisfastenoughhavefunsolv"

Have fun solving this SRM! Watch out for timeouts, make sure your solution is fast enough.

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

Coding Area

Language: C++17 · define a public class OperateString with a public method string operate(string s, vector<int> moves) · 29 test cases · 2 s / 256 MB per case

Submitting as anonymous