문제 풀이

 

순서의 뒤집기를 말 그대로 뒤집지 않고, reverse라는 bool type의 변수를 두어 뒤집어진 경우에는 뒤에서 삭제, 뒤에서 출력을 해주면 문제 풀이가 가능합니다. 그 외에도 strtok라는 함수를 써서 입력을 받으면 한결 간편하게 입력받기가 가능합니다.

 

 

코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <cstring>
#include <deque>
 
using namespace std;
 
deque <int> dq;
char arr[400005];
 
int main() {
 
    int t;
    cin >> t;
 
    while (t--) {
        
        memset(arr, '\0'sizeof(arr));
        bool e = false;
        bool r = false;
        dq.clear();
        string p; int n;
        cin >> p >> n >> arr;
 
        char * tmp = strtok(arr, "[,]");
        while (tmp) {
            dq.push_back(stoi(tmp));
            tmp = strtok(NULL"[,]");
        }
 
        for (int i = 0; i < p.size(); i++) {
            if (p[i] == 'R') {
                if (r) r = false;
                else r = true;
            }
            if (p[i] == 'D') {
                if (dq.empty()) {
                    e = true;
                    break;
                }
                if (r) dq.pop_back();
                else dq.pop_front();
            }
        }
 
        if (e) cout << "error\n";
        else {
            cout << '[';
            for (int i = 0; i < dq.size(); i++) {
                if (r) cout << dq[dq.size() - 1 - i];
                else cout << dq[i];
 
                if (i == dq.size() - 1continue;
                else cout << ',';
            }
            cout << "]\n";
        }
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/5430

 

5430번: AC

문제 선영이는 주말에 할 일이 없어서 새로운 언어 AC를 만들었다. AC는 정수 배열에 연산을 하기 위해 만든 언어이다. 이 언어에는 두 가지 함수 R(뒤집기)과 D(버리기)가 있다. 함수 R은 배열에 있는 숫자의 순서를 뒤집는 함수이고, D는 첫 번째 숫자를 버리는 함수이다. 배열이 비어있는데 D를 사용한 경우에는 에러가 발생한다. 함수는 조합해서 한 번에 사용할 수 있다. 예를 들어, "AB"는 A를 수행한 다음에 바로 이어서 B를 수행하는 함수이다.

www.acmicpc.net

 

문제 풀이

 

빼고자 하는 숫자의 위치를 파악한 후, 2번 연산과 3번 연산 중 더 적게 수행할 수 있는 연산을 수행하여, 1번 연산을 시행하면 문제를 쉽게 풀 수 있습니다.

 

 

코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <deque>
 
using namespace std;
 
int n, m, cnt;
deque <int> dq;
 
void _op2() {
 
    int a = dq.front();
    
    dq.pop_front();
    dq.push_back(a);
    cnt++;
 
    return;
}
 
void _op3() {
 
    int a = dq.back();
 
    dq.pop_back();
    dq.push_front(a);
    cnt++;
 
    return;
}
 
int main() {
 
    cnt = 0;
    cin >> n >> m;
 
    for(int i = 1; i <= n; i++) {
        dq.push_back(i);
    }
 
    while (m--) {
        
        int t = 0;
        int now; cin >> now;
 
        for (int i = 0; i < dq.size(); i++) {
            if (dq[i] == now) {
                t = i;
                break;
            }
        }
 
        if (t < dq.size() - t) {
            for (int i = 0; i < t; i++) {
                _op2();
            }
        }
        else {
            for (int i = 0; i < dq.size() - t; i++) {
                _op3();
            }
        }
        dq.pop_front();
    }
    cout << cnt << '\n';
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/1021

 

1021번: 회전하는 큐

첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가 순서대로 주어진다. 위치는 1보다 크거나 같고, N보다 작거나 같은 자연수이다.

www.acmicpc.net

 

문제 풀이

 

 최소 비용일때마다 fee 배열에 저장된 최소비용을 갱신해줍니다. fee[finish]에 start node에서 finish node로 가는 최소 비용이 저장됩니다.

 

 

코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
 
using namespace std;
 
int n, m, finish;
int fee[1001];
vector <pair<int,int>> graph[1001];
 
int go(int start) {
 
    queue<int> q;
    q.push(start);
    fee[start] = 0;
 
    while (!q.empty()) {
        int x = q.front();
        q.pop();
 
        for (int i = 0; i < graph[x].size(); i++) {
            int nx, cost;
            nx = graph[x][i].first;
            cost = graph[x][i].second;
            if (fee[nx] == -1 || fee[x] + cost < fee[nx]) {
                fee[nx] = fee[x] + cost;
                if (nx != finish) {
                    q.push(nx);
                }
            }
        }
    }
    return fee[finish];
}
 
int main() {
 
    memset(fee, -1sizeof(fee));
    cin >> n >> m;
 
    while (m--) {
        int b, e, c; cin >> b >> e >> c;
        graph[b].push_back(make_pair(e, c));
    }
 
    int start; cin >> start >> finish;
 
    cout << go(start) << '\n';
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/1916

 

1916번: 최소비용 구하기

첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. 버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다. 그리고 M+3째 줄에는 우리가 구하고자 하는 구간

www.acmicpc.net

 

문제 풀이

 

아스키 코드를 활용하여 대문자인 경우 출력해주었습니다.

 

 

코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
 
    string s;
    cin >> s;
 
    for (int i = 0; i < s.size(); i++) {
        
        int a = s[i];
 
        if (i == 0) {
            cout << s[i];
        }
        else if (a >= 65 && a <= 90) {
            cout << s[i];
        }
    }
 
    cout << '\n';
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/2902

 

2902번: KMP는 왜 KMP일까?

문제 KMP 알고리즘이 KMP인 이유는 이를 만든 사람의 성이 Knuth, Morris, Prett이기 때문이다. 이렇게 알고리즘에는 발견한 사람의 성을 따서 이름을 붙이는 경우가 많다. 또 다른 예로, 유명한 비대칭 암호화 알고리즘 RSA는 이를 만든 사람의 이름이 Rivest, Shamir, Adleman이다. 사람들은 이렇게 사람 성이 들어간 알고리즘을 두 가지 형태로 부른다. 첫 번째는 성을 모두 쓰고, 이를 하이픈(-)으로 이어 붙인 것이다. 예

www.acmicpc.net

 

+ Recent posts