結果

問題 No.517 壊れたアクセサリー
ユーザー beetbeet
提出日時 2018-12-20 16:13:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 207,284 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-26 01:03:27
合計ジャッジ時間 3,351 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 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 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<string> s(n);
  for(Int i=0;i<n;i++) cin>>s[i];
  Int m;
  cin>>m;
  vector<string> t(m);
  for(Int i=0;i<m;i++) cin>>t[i];

  auto NG=[&](){
            cout<<-1<<endl;
            exit(0);
          };  
  Int l=0;
  for(string x:s) l+=x.size();
  {
    Int tmp=0;
    for(string x:t) tmp+=x.size();
    if(l!=tmp) NG();
  }
  
  if(l==1){
    cout<<s[0]<<endl;
    return 0;
  }

  vector<Int> nx(26,-1),pr(26,-1);
  for(string x:s){
    for(Int i=0;i+1<(Int)x.size();i++){
      nx[x[i]-'A']=x[i+1]-'A';
      pr[x[i+1]-'A']=x[i]-'A';
    }      
  }
  
  for(string x:t){
    for(Int i=0;i+1<(Int)x.size();i++){
      if(~nx[x[i]-'A']) if(nx[x[i]-'A']!=x[i+1]-'A') NG();
      if(~pr[x[i+1]-'A']) if(pr[x[i+1]-'A']!=x[i]-'A') NG();
      nx[x[i]-'A']=x[i+1]-'A';
      pr[x[i+1]-'A']=x[i]-'A';
    }      
  }

  string ans;
  Int pos=-1;
  for(Int i=0;i<26;i++){
    if(pr[i]>=0||nx[i]<0) continue;
    if(~pos) NG();
    pos=i;
  }

  while(~pos){
    ans.push_back('A'+pos);
    pos=nx[pos];
  }
  if((Int)ans.size()!=l) NG();
  cout<<ans<<endl;
  return 0;
}
0