Lets Help Atom(https://www.codechef.com/CIU2018/problems/C001)- Getting wrong answer

Can Someone please provide more test cases, or tell me for which test case my code got failed and why.

#include<bits/stdc++.h>

using namespace std ;

int main()
{
	string str , inp ;
	int op ;
	stack<string> st ;
	cin >> op ;
	cin.ignore() ;
	while(op--)
	{
		getline(cin , inp) ;
		if(inp[0] == '1')
		{
			st.push(str) ;
			inp.erase(inp.begin(),inp.begin()+2);
			str += inp ;
		}
		if(inp[0] == '2')
		{
			st.push(str) ;
			int len =  inp[2]-'0' ;
			str.erase(str.end() - len , str.end()) ;
		}
		if(inp[0] == '3')
		{
		 cout << str[inp[2] - '0' - 1] << endl;
	    }
		if(inp[0] == '4')
		{
			str.clear() ;
			str = st.top();
			st.pop();
		}
		inp.clear() ;
	}
}

Why are you reading even integer value with string?? How did you know that the index for operation 3 or length for operation 2 is going to be only one digit? You can read integer value to determine operation first and then read string as getline(cin>>ws,str).

Please dont put problem link in title, put it in question. Then the question might be cute :slight_smile:

1 Like