0% found this document useful (0 votes)
461 views

Codechef Assignment 5

This document contains 4 coding practice problems from CodeChef related to stacks, queues, and file system commands: 1) The first problem involves removing adjacent duplicate characters from a string using a stack. 2) The second deals with modeling a book stacking problem using a stack. 3) The third examines chefs in a queue and calculates their cumulative fear based on position. 4) The fourth implements basic file system commands like pwd and cd using a string to represent the file path.

Uploaded by

Kumar Sanu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
461 views

Codechef Assignment 5

This document contains 4 coding practice problems from CodeChef related to stacks, queues, and file system commands: 1) The first problem involves removing adjacent duplicate characters from a string using a stack. 2) The second deals with modeling a book stacking problem using a stack. 3) The third examines chefs in a queue and calculates their cumulative fear based on position. 4) The fourth implements basic file system commands like pwd and cd using a string to represent the file path.

Uploaded by

Kumar Sanu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CODECHEF ASSIGNMENT 5

https://www.codechef.com/CUPA2101

Q. Optimal Adjacent Removal

#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define ss second

#define vi vector<int>

#define rep(i,a,b) for(int i=a;i<b;i++)

#define ff first

int main(){

ll t;

cin>>t;

while(t--){

string s;

cin>>s;

stack<char> st;

for(ll i=0;i<s.length();i++){

if(st.empty()||st.top()!=s[i]){

st.push(s[i]);

}else{

st.pop();

cout<<st.size()<<endl;

return 0;

}
CODECHEF PRACTICE PROBLEM 5
https://www.codechef.com/CUPP2101

Q. Sudhanva and Books


#include <iostream>
#include<stack>
using namespace std;

int main() {
// your code goes here
int n,t,a;
stack<int>stack;
cin>>t;
while(t--)
{
cin>>a;
if(a==1)
{
cin>>n;
stack.push(n);

}
else
{ if(!stack.empty()){
cout<<stack.top()<<endl;
stack.pop();}
else
cout<<"kuchbhi?"<<endl;
}
}
return 0;
}

Q. Chefs in Queue
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define modulo 1000000007
int main() {

ll n, k, i;
cin>>n>>k;
ll A[n];
stack<ll>stk;
for(i=0; i<n; i++){
cin>>A[i];
}
ll fearfull_ness=1;
for(i=0; i<n; i++){
while( !stk.empty() && A[stk.top()] > A[i] ){
fearfull_ness = (fearfull_ness % modulo) * (i-stk.top()+1) % modulo;

stk.pop();
}
stk.push(i);
}
cout<<fearfull_ness;
return 0;
}

Q. Nikhil and Commands


#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s="/";
string wd,cmd;
for(int i=0;i<n;i++){
cin>>wd;
if(wd=="pwd"){
if(s=="/")
cout<<s<<endl;
else cout<<s<<"/"<<endl;
}
if(wd == "cd"){
cin>>cmd;
if(cmd[0]!='/'){
if(s=="/") s+=cmd;
else s=s+"/"+cmd;
}
else{
s=cmd;
}
while(true){
long long int k=s.find('.');
if(k==-1)
break;
string s1=s.substr(0,k-1);
string s2=s.substr(k+2,s.size()-k-2);
k=s1.rfind("/");
s1=s1.substr(0,k);
s=s1+s2;
}
}
}
}
}

You might also like