ANARC09A - Seinfeld
no tags
I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. But, alas, not for this one.
You’re given a non empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition for being stable is as follows:
- An empty string is stable.
- If S is stable, then {S} is also stable.
- If S and T are both stable, then ST (the concatenation of the two) is also stable.
All of these strings are stable: {}, {}{}, and {{}{}}; But none of these: }{, {{}{, nor {}{.
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.
Input
Your program will be tested on one or more data sets. Each data set is described on a single line. The line is a non-empty string of opening and closing braces and nothing else. No string has more than 2000 braces. All sequences are of even length.
The last line of the input is made of one or more ’-’ (minus signs.)
Output
For each test case, print the following line:
k. N
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one.
Example
Input: }{ {}{}{} {{{} --- Output: 1. 2 2. 0 3. 1
##########################editorial###################################################
for each closing bracket (}) it there is unused previous opening bracket than decrease no of opening bracket .. it there is no opening bracket previous than we must need to change this opening bracket to closing so increase count of change and sing this bracket is changed so also make count of opening bracket =1( since it is zero privous ) ..now if found any opening bracket than inc count of opening bracket and final ans will be count+ (no of remain openingbracket)/2..
######################################code###################################
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int l;
int t;
char str[2002];
int x=1;
while(1)
{
cin>>str;
if(str[0]=='-')
break;
else
{
l=strlen(str);
int count=0;
int bal=0;
for(int i=0;i<l;i++)
{
if(str[i]=='}')
count--;
else
count++;
if(count<0)
{
count=1;
bal++;
}
}
cout<<x<<"."<<" "<<bal+count/2<<endl;
x++;}
}
return 0;
}
################################### code 2nd approach #########################
#include<iostream>
char arr[10000001];
#include<bits/stdc++.h>
using namespace std;
int main()
{
cin>>arr;
int t=0;
while(arr[0]!='-')
{
int lm=0;
int rm=0;
int len=strlen(arr);
for(int i=0;i<len;i++)
{
if(arr[i]=='}')
{
if(rm>0) rm--;
else lm++;
}
else if(arr[i]=='{')
{
rm++;
}
}
if(lm%2==0)
{
lm/=2;
rm/=2;
cout<<++t<<". "<<(lm+rm)<<endl;
}
else
{
cout<<++t<<". "<<((lm+rm)/2)+1<<endl;
}
cin>>arr;
}
return 0;
}
No comments:
Post a Comment