Sort by

recency

|

548 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    typescript boilerplate: 'use strict';

    process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString: string = ''; let inputLines: string[] = []; let currentLine: number = 0; process.stdin.on('data', function(inputStdin: string): void { inputString += inputStdin; });

    process.stdin.on('end', function(): void { inputLines = inputString.split('\n'); inputString = ''; main(); });

    function readLine(): string { return inputLines[currentLine++]; }

    class TextEditor { private currentText:string; // Enter your code here constructor(){ this.currentText ="" } append(w:string){ // Enter your code here } delete(k:number){ // Enter your code here } print(k:number){ // Enter your code here } undo(){ // Enter your code here } }

    type EditorMethod = "append" | "delete" | "print" | "undo";

    function main() { const commandMap = {"1": "append", "2": "delete", "3" : "print", "4": "undo" } as const;

    const textEditor = new TextEditor();
    inputLines.shift()
    inputLines.map((inputLine:string) => {
        const [command, input] = inputLine.split(" ") as ["1"|"2"|"3"|"4", string]
        const method = commandMap[command] 
        if (method == "undo") return textEditor.undo()
        if (method == "append") return textEditor.append(input);
    
        return textEditor[method](Number(input))
    } );
    

    }