結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
![]() |
提出日時 | 2018-12-20 16:13:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 2,000 ms |
コード長 | 1,356 bytes |
コンパイル時間 | 2,761 ms |
コンパイル使用メモリ | 199,796 KB |
最終ジャッジ日時 | 2025-01-06 19:44:25 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#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 HEREsigned 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;}