I have been trying to solve a problem which require unknown number of string inputs. Below is my code which is not working as required. I just need help in how to take input of multiple lines.
#include<bits/stdc++.h>
using namespace std;
void solve(string s)
{
// cout << "String is : " << s << endl;
stack<char> stk;
for(int i = 0; s[i] != '\0'; i++)
{
if(s[i] == '[')
stk.push(s[i]);
else if(s[i] == ' ')
{
if(stk.top() == s[i])
stk.pop();
else
stk.push(s[i]);
}
else if(s[i] == ']')
{
if(stk.top() == ' ')
stk.pop();
if(stk.top() == '[')
stk.pop();
}
}
cout << (stk.empty()? "YES" : "NO");
// while(!stk.empty())
// {
// cout << stk.top();
// stk.pop();
// }
// cout << '\n';
}
int main()
{
string s;
while(getline(cin, s))
{
solve(s);
}
}
Input is given below.
Input is : [] [ [ ] ] [ []] [] ] ][
But I am not able to read multiple lines.
As In my code there is a commented line (at the beginning of solve function)
cout << "String is : " << s << endl; . This line not giving output for any string input (Don't worry I know I have to decomment it).
What is the mistake in taking input for the strings?
Thank You..