티스토리 뷰

백준 문제풀이

백준[baekjoon] 14641

소심야채 2020. 1. 11. 18:08
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string.h>

using namespace std;

const int INF = 987654321;

int main()
{
	int m, n;
	bool isConnected[26][26] = { false, };
	scanf("%d %d", &m, &n);
	
	for (int i = 0; i < m; i++) {
		char a, b;
		
		getchar();
		scanf("%c %c", &a, &b);
		int s = a - 'a', e = b - 'a';
		isConnected[s][e] = true;
	}

	for (int k = 0; k < 26; k++) {
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				if (isConnected[i][k] && isConnected[k][j])
					isConnected[i][j] = true;
			}
		}
	}

	for (int iter = 0; iter < n; iter++) {
		char str_a[51], str_b[51];
		getchar();
		scanf("%s %s", &str_a, &str_b);

		int len_a = strlen(str_a), len_b = strlen(str_b);

		bool isPossible = true;

		if (len_a != len_b)
			isPossible = false;
		
		else {
			for (int i = 0; i < len_a; i++) {
				int s = str_a[i] - 'a', e = str_b[i] - 'a';
				
				if (s == e) continue;

				if (!isConnected[s][e]) {
					isPossible = false;
					break;
				}
			}
		}
		printf("%s\n", isPossible ? "yes" : "no");
	}
	return 0;
}

'백준 문제풀이' 카테고리의 다른 글

백준[baekjoon] 8462  (0) 2020.05.27
백준[baekjoon] 14728  (0) 2020.05.02
백준[baekjoon] 12865  (0) 2020.04.20
백준[baekjoon] 10165  (0) 2020.02.26
백준[baekjoon] 1390  (0) 2020.01.19
댓글