Hi
can someone help me to solve this problem.
there are some braces {{}{{}}}
How can I make a program in C# that can tell which opening brace is for which closing brace?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Roy SPosted Aug 21, 2011, 2:36 PM
using System.Collections.Generic;
namespace BraceTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(doBracesWork(Console.ReadLine()));
Console.ReadLine();
}
static string doBracesWork(string input)
{
Stack
string output = "";
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '{')
{
openingBraces.Push(i);
}
else if (input[i] == '}')
{
output += string.Format("{0} - {1}\n", openingBraces.Pop(), i);
}
}
return output;
}
}
}
VulpesPosted Aug 22, 2011, 5:39 AM
VulpesPosted Aug 22, 2011, 5:30 AM
If there's more than one sequence with the same length, then the program returns the first sequence found.
It's only looking for ascending sequences. If you want to check for descending sequences as well then insert another for loop after the first one:
Deepak KumarPosted Aug 22, 2011, 12:35 AM
but i want some more help from your side.
first i don't want to do array sort, i want the sum of sub maximum sequential array. not maximum sum of sequential array.
like if array {1,2,3,4,10,11,5,21,22}
then i want the sum of 1,2,3,4 not {21,22} maximum sum of sequential array . I want the sum of largest sub array in length .
Please provide the answer as soon as possible .
Thanx in advance .
VulpesPosted Aug 21, 2011, 7:41 PM
In response to your other question, try the following:
sunakshi saxenaPosted Aug 21, 2011, 3:57 PM
That is suppose if i have a array like 1,2,3,4,5,6,7,8,9,10,11 than how can i get the sum of biggest sequential array from this array like if biggest sequential array from this is 8,9,10,11 hai so how can i get the sum.
sunakshi saxenaPosted Aug 21, 2011, 2:29 PM
sunakshi saxenaPosted Aug 21, 2011, 2:29 PM
Roy SPosted Aug 21, 2011, 11:23 AM
input:
"{{}{{}}}"
output:
1 - 2
4 - 5
3 - 6
0 - 7
I would store the indices of the openingbraces in a stack and when a closing brace is found then you know that the last opening brace that was found is paired with the current closing brace.
string doBracesWork(string input)
{
Stack
string output = "";
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '{')
{
openingBraces.Push(i);
}
else if (input[i] == '}')
{
output += string.Format("{0} - {1}\n", openingBraces.Pop(), i);
}
}
return output;
}
Vilas GitePosted Aug 21, 2011, 11:00 AM
Hi Sunakshi!
In Visual Studio, at coding part when you typed braces then its automatically highlighted for itself.