結果

問題 No.517 壊れたアクセサリー
ユーザー Shun_PIShun_PI
提出日時 2017-05-28 21:54:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 3,191 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 80,540 KB
実行使用メモリ 53,492 KB
最終ジャッジ日時 2023-10-21 14:09:56
合計ジャッジ時間 4,480 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,480 KB
testcase_01 AC 59 ms
53,476 KB
testcase_02 AC 54 ms
53,248 KB
testcase_03 AC 62 ms
53,488 KB
testcase_04 AC 53 ms
53,264 KB
testcase_05 AC 57 ms
53,428 KB
testcase_06 AC 52 ms
53,260 KB
testcase_07 AC 57 ms
53,480 KB
testcase_08 AC 57 ms
53,492 KB
testcase_09 AC 58 ms
52,400 KB
testcase_10 AC 58 ms
53,492 KB
testcase_11 AC 58 ms
53,480 KB
testcase_12 AC 58 ms
53,472 KB
testcase_13 AC 57 ms
53,488 KB
testcase_14 AC 58 ms
53,472 KB
testcase_15 AC 60 ms
53,476 KB
testcase_16 AC 58 ms
53,492 KB
testcase_17 AC 58 ms
53,476 KB
testcase_18 AC 59 ms
53,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.NoSuchElementException;

public class Main {
	private static FastScanner sc = new FastScanner();

	public static void main(String[] args) {
		HashMap<Character, Integer> mojimap = new HashMap<>();
		HashMap<Integer, Character> mojimap2 = new HashMap<>();
		HashMap<Integer, Integer> setuzoku = new HashMap<>();
		
		int N = sc.nextInt();
		int idx = 0;
		for(int i=0; i<N; i++) {
			char[] str = sc.next().toCharArray();
			for(int j=0; j<str.length; j++) {
				mojimap.put(str[j], idx);
				mojimap2.put(idx, str[j]);
				if(j != str.length - 1) {
					setuzoku.put(idx, idx+1);
				}
				idx++;
				
			}
		}
		
		int length = idx;
		
		if(length == 1) {
			System.out.println(mojimap2.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 bef_idx = mojimap.get(str[j]);
				int next_idx = mojimap.get(str[j+1]);
				if(setuzoku.containsKey(bef_idx) && setuzoku.get(bef_idx) != next_idx) {
					System.out.println(-1);
					return;
				} else if(!setuzoku.containsKey(bef_idx)) {
					setuzoku.put(bef_idx, next_idx);
				}
			}
		}
		
		for(int i=0; i<length; i++) {
			int count = 0;
			idx = i;
			boolean flag = false;
			String str = "";
			while(true) {
				
				if(count > length) {
					break;
				}
				if(!setuzoku.containsKey(idx)) {
					if(count == length - 1) {
						str = str + mojimap2.get(idx);
						flag = true;
						break;
					} else {
						break;
					}
				}
				str = str + mojimap2.get(idx);
				idx = setuzoku.get(idx);
				count++;
				
			}
			
			if(flag) {
				System.out.println(str);
				return;
			}
		}
		
		System.out.println(-1);
		
	}

	static class FastScanner {
	    private final InputStream in = System.in;
	    private final byte[] buffer = new byte[1024];
	    private int ptr = 0;
	    private int buflen = 0;
	    private boolean hasNextByte() {
	        if (ptr < buflen) {
	            return true;
	        }else{
	            ptr = 0;
	            try {
	                buflen = in.read(buffer);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            if (buflen <= 0) {
	                return false;
	            }
	        }
	        return true;
	    }
	    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
	    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
	    public String next() {
	        if (!hasNext()) throw new NoSuchElementException();
	        StringBuilder sb = new StringBuilder();
	        int b = readByte();
	        while(isPrintableChar(b)) {
	            sb.appendCodePoint(b);
	            b = readByte();
	        }
	        return sb.toString();
	    }
	    public long nextLong() {
	        return Long.parseLong(next());
	    }
	    public int nextInt(){
	    	return Integer.parseInt(next());
	    }
	}
}
0