Advertisement
stanevplamen

03.02.03.Brackets

Jul 26th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2.  
  3. class Brackets
  4. {
  5.     static void Main()
  6.     {
  7.         string inputExpression = Console.ReadLine(); //"";
  8.         int lastIndex = inputExpression.LastIndexOf("(");
  9.         int allLength = inputExpression.Length;
  10.  
  11.         if (lastIndex == allLength - 1 || inputExpression[0] == ')' || allLength % 2 != 0)
  12.         {
  13.             Console.WriteLine(0);
  14.             Environment.Exit(0);
  15.         }
  16.  
  17.         long[,] resultMatrix = new long[allLength + 1, allLength + 1];
  18.         resultMatrix[0,0] = 1;
  19.  
  20.         for (int row = 1; row < resultMatrix.GetLength(0); row++)
  21.         {
  22.             for (int col = 0; col < resultMatrix.GetLength(1); col++)
  23.             {
  24.                 if (inputExpression[row-1] == '?')
  25.                 {
  26.                     if (col - 1 < 0)
  27.                     {
  28.                         resultMatrix[row, col] = resultMatrix[row - 1, col + 1];
  29.                     }
  30.                     else if (col + 1 >= resultMatrix.GetLength(1))
  31.                     {
  32.                         resultMatrix[row, col] = resultMatrix[row - 1, col - 1];
  33.                     }
  34.                     else
  35.                     {
  36.                         resultMatrix[row, col] = resultMatrix[row - 1, col - 1] + resultMatrix[row - 1, col + 1];
  37.                     }
  38.                 }
  39.                 else if (inputExpression[row - 1] == '(')
  40.                 {
  41.                     if (col - 1 < 0)
  42.                     {
  43.                         resultMatrix[row, col] = 0;
  44.                     }
  45.                     else
  46.                     {
  47.                         resultMatrix[row, col] = resultMatrix[row - 1, col - 1];
  48.                     }
  49.                 }
  50.                 else if (inputExpression[row - 1] == ')')
  51.                 {
  52.  
  53.                     if (col + 1 >= resultMatrix.GetLength(1))
  54.                     {
  55.                         resultMatrix[row, col] = 0;
  56.                     }
  57.                     else
  58.                     {
  59.                         resultMatrix[row, col] = resultMatrix[row - 1, col + 1];
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         Console.WriteLine(resultMatrix[resultMatrix.GetLength(0)-1, 0]);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement