[PS 백준 - 1.8] 2804번: 크로스워드 만들기

PongkiJoa·2021년 6월 29일
0

PS Diary - 백준

목록 보기
9/54
post-thumbnail

문제 정보

백준 2804번 - 바로가기

  • 난이도: 브론즈 2
  • 알고리즘: 문자열

코멘트

먼저 겹치는 단어를 찾고, 해당 단어의 좌표의 상하좌우에 단어를 출력하면 된다. 처음에는 어려워 보였지만 생각보다 구현이 간단한 문제였다.


소스 코드

#include <iostream>
#include <deque>
#include <algorithm>
#include <string>

using namespace std;

int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	std::ios::sync_with_stdio(false);

	string s1, s2;

	cin >> s1 >> s2;
	
	char target;
	bool flag = false;
	for (char c1 : s1) {
		for (char c2 : s2) {
			if (c1 == c2) {
				target = c1;
				flag = true;
			}
			if (flag) break;
		}
		if (flag) break;
	}

	int x = s2.find(target);
	int y = s1.find(target);
	
	for (int i = 0; i < s2.length(); i++) {
		for (int j = 0; j < s1.length(); j++) {
			if (i == x) { 
				cout << s1[j];
			}
			else if (j == y) {
				cout << s2[i];
			}
			else {
				cout << ".";
			}
		}
		cout << "\n";
	}

}
profile
컴공 20학번

0개의 댓글

관련 채용 정보