Sort by

recency

|

549 Discussions

|

  • + 0 comments
    my c++ sol:
    
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        string s="";
        stack<string> st;
        int n;
        cin>>n;
        for(int i =0; i< n; i++)
        {
            int k =0;
            cin>>k;
            if(k==1)
            {
                string str;
                cin>>str;
                st.push(s);
                s.append(str);
            }
            if(k==2)//last k character
            {
                int no;
                cin>>no;
                st.push(s);
                while(no>0)
                {
                    s.pop_back();
                    no--;
                }
            }
            if(k==3)//print kth char
            {
                int h;
                cin>>h;
                cout<<s[h-1]<<endl;
            }
            else if (k == 4) {
                if (!st.empty()) {
                    s = st.top();
                    st.pop();
                }
            }
        }
        return 0;
    }
    
  • + 0 comments

    My C++ Solution:

    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int q=0;
        cin >> q;
        string s ="";
        stack<string> v;
        v.push(s);
        for(int i=0; i<q; i++){
            int op = 0;string ss;
            cin >> op;
            if(op != 4)cin >> ss;
            switch (op) {
                case 1:
                   s+=ss;
                   v.push(s);
                   break;
                case 2:
                    s = s.substr(0, s.length() - stoi(ss));
                    v.push(s);
                    break;
                 case 3:
                    cout << v.top()[stoi(ss)-1]<<endl;
                    break;
                case 4:
                    v.pop();
                    s = v.top();
                    break;
            }
        }
        
        return 0;
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
          
            Scanner sc = new Scanner(System.in);
            String str = "";
            int top = 0;
            int q = Integer.parseInt(sc.nextLine());
            MyStack stack = new MyStack(q);
            for(int i = 0; i < q; ++i){
                String st[] = sc.nextLine().split(" ");
                int query = Integer.parseInt(st[0]);
                if(query == 1){
                    Node newNode = new Node(query,str.length());
                    stack.top++;
                    stack.list[stack.top] = newNode;
                    str += st[1];
                } else if(query == 2){
                    int k = Integer.parseInt(st[1]);
                    Node newNode = new Node(query,str.substring(str.length()-k));
                    stack.top++;
                    stack.list[stack.top] = newNode;
                    str = str.substring(0,str.length()-k);
                } else if(query == 3){
                    int index = Integer.parseInt(st[1]);
                    System.out.println(str.charAt(index-1));
                } else if(query == 4){
                    Node newNode = stack.list[stack.top];
                    stack.top--;
                    if(newNode.qtype == 1){
                        str = str.substring(0,newNode.idx);
                    } else if(newNode.qtype == 2){
                        str += newNode.w;
                    }
                }
            }
        }
    }
    class MyStack{
        Node list[];
        int top;
        MyStack(int size){
            this.list = new Node[size];
            this.top = -1;
        }
    }
    class Node{
        int qtype;
        int idx;
        String w;
        Node(int x, String y){
            this.qtype = x;
            this.w = y;
        }
        Node(int x, int index){
            this.qtype = x;
            this.idx = index;
        }
    }
    
  • + 0 comments

    C++

    include

    include

    include

    include

    include

    include

    using namespace std;

    static stack sString{}; void append(string &s,string appStr){ sString.push(s); s=s+appStr; //cout<<"Appended: "<

    void del( int k, string &s){ sString.push(s); if(s.size()-k >=0) s.erase(s.size()-k,k); //cout<<"Deleted: "<

    void print(int k,const string &s){ cout << s[k-1]<

    void undo(string &s){

    s=sString.top();
    sString.pop();
    //cout<<"After Undo: "<<s<<endl;
    

    }

    vector splitString(string s,string delimiter) { vector out{}; int start=0; int end=s.find(delimiter,start); while(end!=string::npos) { out.push_back(s.substr(start,end-start)); start=end+delimiter.length(); end=s.find(delimiter,start); } out.push_back(s.substr(start)); return out; }

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int q; cin>>q; string out{}; while (q>=0) { string s; getline(cin,s); vector split = splitString(s, " ");

        if(split[0]=="1")
        {
            append(out, split[1]);
        }
        else if(split[0]=="2")
        {
            del(stoi(split[1]), out);
        }
        else if(split[0]=="3")
        {
            print(stoi(split[1]), out);
        }
        else if(split[0]=="4")
        {
            undo(out);
        }
        q--;
    }    
    return 0;
    

    }

  • + 0 comments

    C++

    include

    include

    include

    include

    include

    include

    using namespace std;

    static stack sString{}; void append(string &s,string appStr){ sString.push(s); s=s+appStr; //cout<<"Appended: "<

    void del( int k, string &s){ sString.push(s); if(s.size()-k >=0) s.erase(s.size()-k,k); //cout<<"Deleted: "<

    void print(int k,const string &s){ cout << s[k-1]<

    void undo(string &s){

    s=sString.top();
    sString.pop();
    //cout<<"After Undo: "<<s<<endl;
    

    }

    vector splitString(string s,string delimiter) { vector out{}; int start=0; int end=s.find(delimiter,start); while(end!=string::npos) { out.push_back(s.substr(start,end-start)); start=end+delimiter.length(); end=s.find(delimiter,start); } out.push_back(s.substr(start)); return out; }

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int q; cin>>q; string out{}; while (q>=0) { string s; getline(cin,s); vector split = splitString(s, " ");

        if(split[0]=="1")
        {
            append(out, split[1]);
        }
        else if(split[0]=="2")
        {
            del(stoi(split[1]), out);
        }
        else if(split[0]=="3")
        {
            print(stoi(split[1]), out);
        }
        else if(split[0]=="4")
        {
            undo(out);
        }
        q--;
    }    
    return 0;
    

    }