문자열 알고리즘

Date:

1. Stack을 이용한 방법

#include <string>
#include <vector>
#include <stack>
#include <iostream>

using namespace std;

long long solution(vector<int> a, vector<vector<int>> edges) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long answer = -2;
    vector<vector<string>> map(13);
    map.clear();
    int num = 0;
    for(int i = 0; i < 13; i++) {
        for(int j = 0; j < 13; j++) {
            string s = to_string(++num);
            if(s.length() == 1) {
                s = "00" + s;
            }else if(s.length() == 2) {
                s = "0" + s;
            }
            map[i].push_back(s);
            
        }
    }
    
    vector<int> rectangle = {1,2,8,10};
    int r1 = rectangle[0];
    int c1 = rectangle[1];
    int r2 = rectangle[2];
    int c2 = rectangle[3];
    
    for(int i = r1 + 1; i <= r2; i++) {
        int x = i;
        stack<string> s;
        while(x >= r1) {
            s.push(map[x][i+c1-x]);
            x--;
        }
        x = i;
        while(!s.empty()) {
            map[x][i+c1-x] = s.top();
            s.pop();
            x--;
        }
    }
    
    for(int i = c1 + 1; i <= c2; i++) {
        int x = i;
        stack<string> s;
        while(x <= c2) {
            s.push(map[i+r2-x][x]);
            x++;
        }
        x = i;
        while(!s.empty()) {
            map[i+r2-x][x] = s.top();
            s.pop();
            x++;
        }
    }
    
    for(int i = 0; i < 13; i++) {
        for(int j = 0; j < 13; j++) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    
    return answer;
}

댓글