Algorithm(CodeTree, C++)/문자열

[코드트리] 문자열 놀이 C++

kurooru 2024. 6. 17. 20:21

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

void simulate(string &str, int order, char op1, char op2) {
    if (order == 1) {
        char temp = str[op1 - 1];
        str[op1 - 1] = str[op2 - 1];
        str[op2 - 1] = temp; 
    } else if (order == 2) {
        int str_len = str.length();
        for (int i = 0; i < str_len; i++) {
            if (str[i] == op1) {
                str[i] = op2;
            }
        }
    }
    cout << str << endl;
}

int main() {
    string str;
    int n;
    cin >> str >> n;

    int order;
    char op1, op2;
    for (int i = 0; i < n; i++) {
        cin >> order;
        if (order == 1) {
            int pos1, pos2;
            cin >> pos1 >> pos2;
            simulate(str, order, pos1, pos2);
        } else if (order == 2) {
            cin >> op1 >> op2;
            simulate(str, order, op1, op2);
        }
    }

    return 0;
}
 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai