The problem statement is as follows
PS: Ignore the repeated input format in the problem statement.
Here is my code
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve (int n, int q, string s ) {
for(int i = 0;i<q;i++){
string query;
cin>>query;
if(query =="pop_back"){
s.pop_back();
n--;
}
else if(query =="front"){
cout<<s[0]<<'n';
}
else if(query=="back"){
cout<<s[n-1]<<'n';
}
else if (query=="sort"){
int l,r;
cin>>l>>r;
if(l>r){
swap(l,r);
}
sort(s.begin()+l-1,s.begin()+r);
}
else if (query=="reverse"){
int l,r;
if(l>r){
swap(l,r);
}
reverse(s.begin()+l-1,s.begin()+r);
cout<<s;
}
else if (query=="print") {
int a;
cin>>a;
cout<<s[a-1]<<'n';
}
else if (query=="substr"){
int l,r;
cin>>l>>r;
if (l>r){
swap(l,r);
}
string h = s.substr(l-1, r-l+1);
cout<<h<<'n';
}
else {
char x;
cin>>x;
s.push_back(x);
n++;
}
}
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,q;
cin>>n>>q;
string s;
cin>>s;
solve(n,q,s);
}
This code doesnt’ work properly.
After debugging at my end I found that the reverse function is not working as expected.
For the input
10 1
Buzzworthy
reverse 5 6
The output obtained is the same word itself.
I cannot figure out why this is happening.
Apart from this if any more bugs are found please tell them to me.