結果

問題 No.517 壊れたアクセサリー
ユーザー yukudoyukudo
提出日時 2017-05-28 21:57:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,553 bytes
コンパイル時間 1,275 ms
コンパイル使用メモリ 162,240 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:10:42
合計ジャッジ時間 2,110 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for (int i=0,_n=(int)(n); i < _n; i++)
template<class T> bool chkmin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool chkmax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;


bool g[30][30];
bool use[30];

int main2() {
  memset(g, 0, sizeof(g));
  memset(use, 0, sizeof(use));

  int N; cin >> N;
  REP(nnn, N) {
    string s; cin >> s;
    REP(i, s.size()) use[ s[i] - 'A'] = true;
    for (int i = 0; i + 1 < (int)s.size(); i++) {
      int a = s[i] - 'A';
      int b = s[i+1] - 'A';
      g[a][b] = true;
    }
  }
  int M; cin >> M;

  REP(mmm, M) {
    string s; cin >> s;
    REP(i, s.size()) use[ s[i] - 'A'] = true;

    for (int i = 0; i + 1 < (int)s.size(); i++) {
      int a = s[i] - 'A';
      int b = s[i+1] - 'A';
      g[a][b] = true;
    }
  }

  // REP(i, 26) if (use[i]) {
  //   REP(j, 26) if (use[j]) {
  //     cout << g[i][j];
  //   }
  //   cout << endl;
  // }

  int first = -1;
  for (int i = 0; i < 26; i++) if (use[i]) {
    int indeg = 0;
    REP(j, 26) if (use[j]) {
      if (g[j][i]) indeg++;
    }
    if (indeg == 0) {
      if (first != -1) { cout << -1 << endl; return 0; }
      first = i;
    }
  }
  string ans = "";
  int p = first;
  for (;;) {
    ans += (char)('A' + p);
    int next = -1;
    REP(j, 26) if (g[p][j]) { next = j; break; }
    if (next == -1) break;
    p = next;
  }
  cout << ans << endl;

  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws) main2();
  return 0;
}
0