結果

問題 No.517 壊れたアクセサリー
ユーザー Daigo HIROOKADaigo HIROOKA
提出日時 2018-06-30 02:01:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 3,574 ms
コンパイル使用メモリ 77,180 KB
実行使用メモリ 56,448 KB
最終ジャッジ日時 2023-09-13 16:07:35
合計ジャッジ時間 6,957 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,616 KB
testcase_01 AC 123 ms
55,500 KB
testcase_02 AC 122 ms
56,448 KB
testcase_03 AC 123 ms
55,968 KB
testcase_04 AC 122 ms
55,700 KB
testcase_05 AC 126 ms
55,384 KB
testcase_06 AC 121 ms
55,492 KB
testcase_07 AC 122 ms
55,904 KB
testcase_08 AC 123 ms
55,384 KB
testcase_09 AC 124 ms
55,736 KB
testcase_10 AC 123 ms
55,752 KB
testcase_11 AC 123 ms
56,152 KB
testcase_12 AC 131 ms
55,724 KB
testcase_13 AC 124 ms
55,876 KB
testcase_14 AC 126 ms
55,968 KB
testcase_15 AC 130 ms
55,840 KB
testcase_16 AC 128 ms
55,732 KB
testcase_17 AC 128 ms
55,696 KB
testcase_18 AC 129 ms
55,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
	}
}
0