LA 8047. ACM-ICPC World Finals 2017 I. Secret Chamber at Mount Rushmore

#include <iostream>
#include <cstring>
using std::cin;
using std::cout;

int main() {
  int n, m;
  cin >> n >> m;
  bool G[26][26];
  while (n--) {
    char a, b;
    cin >> a >> b;
    G[a - 'a'][b - 'a'] = true;
  }
  
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++)
        if (G[i][k] & G[k][j])
          G[i][j] = true;
  
  while (m--) {
    char s[51], t[51];
    cin >> s >> t;
    
    int len = strlen(s);
    if (len != strlen(t)) {
      cout << "no\n";
      continue;
    }
    
    for (int i = 0; i < len; i++) 
      if (s[i] != t[i] && !G[ s[i] - 'a' ][ t[i] - 'a' ]) {
        cout << "no\n";
        goto noans;
      }
    
    cout << "yes\n";
    noans:;
  }
  return 0;
}
posted @ 2017-12-28 20:58  Planet6174  阅读(299)  评论(0编辑  收藏  举报