[BOJ/C++] 3048 개미

Hanbi·2022년 4월 4일
0

Problem Solving

목록 보기
11/108
post-thumbnail

문제

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

풀이

string 라이브러리 함수, 알고리즘 라이브러리 함수 이용하면 쉽게 풀 수 있는 문제

reverse(str.begin(), str.end()); //문자열 뒤집기
str.find(array[i]); //str에서 array[i] 찾기
					//있으면 index, 없으면 string::npos를 return
swap(i, i+1); //자리 바꾸기

코드

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(void) {
	int g1_num, g2_num, T, time = 0;
	string g1, g2;
	string ant;
	
	cin >> g1_num >> g2_num;
	cin >> g1;
	cin >> g2;
	cin >> T;
	
	reverse(g1.begin(), g1.end());
	ant = g1 + g2;
	
	while(time < T) {
		for(int i=0; i<g1_num+g2_num-1; i++) {
			if(g1.find(ant[i]) != string::npos && g2.find(ant[i+1]) != string::npos) {
				swap(ant[i], ant[i+1]);
				i++; //swap했을 때는 i++한번 더 해주기 
			}
		}
		time++;
	}
	
	cout << ant;
	
	return 0;
}
profile
👩🏻‍💻

0개의 댓글