-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathONP.cs
64 lines (55 loc) · 2.23 KB
/
ONP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
// https://www.spoj.com/problems/ONP/ #parsing #recursion #stack #strings
// Returns the RPN form of the expression with binary operators and parentheses.
public static class ONP
{
public static string Solve(string expression)
{
// expression is some constant.
if (expression.Length == 1)
return expression;
// expression is of the form (firstSubexpression@secondSubexpression), where @ represents
// an arbitrary operator. Now RPN(expression) = RPN(firstSubexpression)RPN(secondSubexpression)@,
// so our job is just to identify the substrings of the two subexpressions, and solve recursively.
// Starts at index 1, immediately after the ( at the 0th index, and has some calculated length.
string firstSubexpression = expression.Substring(
startIndex: 1,
length: GetSubexpressionLength(expression, subexpressionStartIndex: 1));
// Starts at the index after (firstSubexpression@, and runs up to but not including the ending ).
string secondSubexpression = expression.Substring(
startIndex: 1 + firstSubexpression.Length + 1,
length: expression.Length - (1 + firstSubexpression.Length + 1) - 1);
char @operator = expression[1 + firstSubexpression.Length];
return Solve(firstSubexpression) + Solve(secondSubexpression) + @operator;
}
private static int GetSubexpressionLength(string expression, int subexpressionStartIndex)
{
int currentIndex = subexpressionStartIndex;
int unmatchedParentheses = 0;
do
{
if (expression[currentIndex] == '(')
{
++unmatchedParentheses;
}
else if (expression[currentIndex] == ')')
{
--unmatchedParentheses;
}
++currentIndex;
} while (unmatchedParentheses != 0);
return currentIndex - subexpressionStartIndex;
}
}
public static class Program
{
private static void Main()
{
int remainingTestCases = int.Parse(Console.ReadLine());
while (remainingTestCases-- > 0)
{
Console.WriteLine(
ONP.Solve(Console.ReadLine()));
}
}
}