Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 12. String Explosion
- Explosions are marked with '>'. Immediately after the mark, there will be an integer, which signifies the strength of the explosion.
- You should remove x characters (where x is the strength of the explosion), starting after the punch character ('>').
- If you find another explosion mark ('>') while you’re deleting characters, you should add the strength to your previous explosion.
- When all characters are processed, print the string without the deleted characters.
- You should not delete the explosion character – '>', but you should delete the integers, which represent the strength.
- Input
- You will receive single line with the string.
- Output
- Print what is left from the string after explosions.
- Constraints
- • You will always receive a strength for the punches
- • The path will consist only of letters from the Latin alphabet, integers and the char '>'
- • The strength of the punches will be in the interval [0…9]
- Examples
- Input Output Comments
- abv>1>1>2>2asdasd abv>>>>dasd 1st explosion is at index 3 and it is with strength of 1. We delete only the digit
- after the explosion character. The string will look like this: abv>>1>2>2asdasd
- 2nd explosion is with strength one and the string transforms to this: abv>>>2>2asdasd
- 3rd explosion is now with strength of 2. We delete the digit and we find another
- explosion. At this point the string looks like this: abv>>>>2asdasd.
- 4th explosion is with strength 2. We have 1 strength left from the previous explosion,
- we add the strength of the current explosion to what is left and that adds up to a
- total strength of 3. We delete the next three characters and we receive the string
- abv>>>>dasd. We do not have any more explosions and we print the result: abv>>>>dasd.
- pesho>2sis>1a>2akarate>4hexmaster pesho>is>a>karate>master
- using System;
- namespace _StringExplosion
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- var stringExplosion = Console.ReadLine(); //Genius way
- var strength = 0;
- for (int i = 0; i < stringExplosion.Length; i++)
- {
- if (stringExplosion[i] != '>' && strength > 0)
- {
- stringExplosion = stringExplosion.Remove(i, 1);
- strength--;
- i--;
- }
- else if (stringExplosion[i] == '>')
- {
- strength += int.Parse(stringExplosion[i + 1].ToString());
- }
- }
- Console.WriteLine(stringExplosion);
- //var stringExplosion = Console.ReadLine().Split('>'); //Hard way
- //var listOutput = new List<string>();
- //var strength = 0;
- //var reserveStrength = 0;
- //var counter = 0;
- //foreach (var item in stringExplosion)
- //{
- // var digit = 0;
- // counter++;
- // if (Char.IsDigit(item[0]))
- // {
- // digit = int.Parse(item[0].ToString());
- // if (digit <= 1)
- // {
- // if (item.Length > 1)
- // {
- // var newItem = item.Remove(0, strength);
- // listOutput.Insert(counter - 1, newItem + '>');
- // }
- // else
- // {
- // listOutput.Add(">");
- // }
- // continue;
- // }
- // else
- // {
- // strength = digit - 1;
- // if (item.Length > 1 && strength > 0)
- // {
- // var newItem = item.Remove(0, strength + reserveStrength + 1);
- // if (stringExplosion.Length != counter)
- // {
- // listOutput.Insert(counter - 1, newItem + '>');
- // }
- // else
- // {
- // listOutput.Insert(counter - 1, newItem );
- // }
- // }
- // else
- // {
- // reserveStrength += digit - 1;
- // listOutput.Add(">");
- // }
- // }
- // }
- // else
- // {
- // listOutput.Add(item + '>');
- // }
- //}
- //Console.WriteLine(string.Join("", listOutput));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment