結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
![]() |
提出日時 | 2017-05-28 21:54:12 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 59 ms / 2,000 ms |
コード長 | 3,191 bytes |
コンパイル時間 | 2,943 ms |
コンパイル使用メモリ | 80,680 KB |
実行使用メモリ | 37,136 KB |
最終ジャッジ日時 | 2024-09-21 15:24:20 |
合計ジャッジ時間 | 4,314 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
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()); } } }