반응형
문제 출처 :
https://www.acmicpc.net/problem/3048
알고리즘 분석 :
문제 해결에 필요한 사항
1. 시뮬레이션
개미가 움직이는 루트를 파악해야 한다.
그룹으로 나누어 생각하였는데 처음 입력되는 개미는 1번 그룹, 두번째 입력되는 개미는 2번 그룹 개미로 생각하고
일단 두 개미 집단을 하나의 통로에 넣기위해 문자열을 합쳐야 한다.
그러기 위해 첫번째 그룹 개미의 선두가 뒤집혀있으니 reverse로 뒤집어준다.
그 후 조건에 맞게 swap을 해주면서 개미를 이동시킨다.
소스 코드 :
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 | #include <iostream> #include <cstdio> #include <string> #include <algorithm> using namespace std; typedef pair<char, int> pci; int main() { string str1, str2; pci str[50]; int n, m, time; scanf("%d %d", &n, &m); cin >> str1 >> str2; reverse(str1.begin(), str1.end()); scanf("%d", &time); for (int i = 0; i < n; i++) { str[i].first = str1[i]; str[i].second = 1; } for (int i = n; i < m + n; i++) { str[i].first = str2[i-n]; str[i].second = 2; } while (time--) for (int i = 0; i < n + m; i++) if (i + 1 < n + m && str[i].second == 1 && str[i + 1].second == 2) { swap(str[i], str[i + 1]); i++; } for (int i = 0; i < n + m; i++) printf("%c", str[i].first); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[5639번] 이진 검색 트리 (0) | 2017.04.13 |
---|---|
[2240번] 자두 나무 (2) | 2017.04.13 |
[11404번] 플로이드 (2) | 2017.04.12 |
[8979번] 올림픽 (0) | 2017.04.12 |
[10868번] 최소값 (0) | 2017.04.12 |