結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
|
提出日時 | 2018-06-30 02:01:58 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 138 ms / 2,000 ms |
コード長 | 1,677 bytes |
コンパイル時間 | 4,007 ms |
コンパイル使用メモリ | 80,028 KB |
実行使用メモリ | 54,348 KB |
最終ジャッジ日時 | 2024-07-01 00:46:52 |
合計ジャッジ時間 | 7,264 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
import java.util.*; public class No517{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); HashMap<Character, Integer> map1 = new HashMap<>(); HashMap<Integer, Character> map2 = new HashMap<>(); HashMap<Integer, Integer> connect = new HashMap<>(); int idx = 0; for(int i = 0; i < n; i++){ char[] str = sc.next().toCharArray(); for(int j = 0; j < str.length; j++){ map1.put(str[j], idx); map2.put(idx, str[j]); if(j != str.length-1){ connect.put(idx, idx+1); } idx++; } } int length = idx; if(length == 1){ System.out.println(map2.get(0)); return; } int m = sc.nextInt(); for(int i = 0; i < m; i++){ char[] str = sc.next().toCharArray(); for(int j = 0; j < str.length-1; j++){ int idx_before = map1.get(str[j]); int idx_after = map1.get(str[j+1]); if(connect.containsKey(idx_before) && connect.get(idx_before) != idx_after){ System.out.println(-1); return; }else if(!connect.containsKey(idx_before)){ connect.put(idx_before, idx_after); } } } // System.out.println(Arrays.toString(a)); // System.out.println(Arrays.toString(b)); for(int i = 0; i < length; i++){ int count = 0; idx = i; boolean flg = false; String s = ""; while(true){ if(count > length) break; if(!connect.containsKey(idx)){ if(count == length-1){ s += map2.get(idx); flg = true; break; }else{ break; } } s += map2.get(idx); idx = connect.get(idx); count++; } if(flg){ System.out.println(s); return; } } System.out.println(-1); } }