結果
問題 | No.517 壊れたアクセサリー |
ユーザー | yuya178 |
提出日時 | 2017-06-18 03:31:52 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 57 ms / 2,000 ms |
コード長 | 3,892 bytes |
コンパイル時間 | 2,385 ms |
コンパイル使用メモリ | 78,000 KB |
実行使用メモリ | 50,452 KB |
最終ジャッジ日時 | 2024-10-01 12:13:52 |
合計ジャッジ時間 | 4,504 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
50,280 KB |
testcase_01 | AC | 54 ms
50,448 KB |
testcase_02 | AC | 51 ms
50,436 KB |
testcase_03 | AC | 53 ms
50,400 KB |
testcase_04 | AC | 53 ms
50,080 KB |
testcase_05 | AC | 55 ms
50,236 KB |
testcase_06 | AC | 55 ms
49,980 KB |
testcase_07 | AC | 54 ms
50,012 KB |
testcase_08 | AC | 54 ms
50,412 KB |
testcase_09 | AC | 54 ms
50,444 KB |
testcase_10 | AC | 55 ms
50,208 KB |
testcase_11 | AC | 57 ms
50,452 KB |
testcase_12 | AC | 53 ms
50,188 KB |
testcase_13 | AC | 51 ms
50,416 KB |
testcase_14 | AC | 53 ms
50,340 KB |
testcase_15 | AC | 55 ms
50,236 KB |
testcase_16 | AC | 56 ms
50,396 KB |
testcase_17 | AC | 54 ms
50,024 KB |
testcase_18 | AC | 55 ms
50,232 KB |
ソースコード
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.OutputStream; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); String s[] = new String[n]; int ds[] = new int[26]; int ds1[] = new int[26]; boolean tonari[][] = new boolean[26][26]; Arrays.fill(ds,-1); Arrays.fill(ds1,-1); for(int i=0; i<n; i++){ s[i] = in.next(); ds1[s[i].charAt(s[i].length()-1)-'A'] = s[i].charAt(0)-'A'; for(int j=1; j<s[i].length(); j++){ ds[s[i].charAt(s[i].length()-j)-'A'] = s[i].charAt(0)-'A'; tonari[s[i].charAt(j-1)-'A'][s[i].charAt(j)-'A']= true; } } int m = in.nextInt(); String s1[] = new String[m]; if(n==1 && m==1){ out.println(s[0]); return; } for(int i=0; i<m; i++){ s1[i] = in.next(); if(ds1[s1[i].charAt(s1[i].length()-1)-'A'] == s1[i].charAt(0)-'A'){ out.println("-1"); return; } for(int j=1; j<s1[i].length(); j++){ ds[s1[i].charAt(s1[i].length()-j)-'A'] = s1[i].charAt(0)-'A'; tonari[s1[i].charAt(j-1)-'A'][s1[i].charAt(j)-'A']= true; } } int root = -1; int res = -1; int tmp = -1; for(int i=0; i<26; i++){ if(ds[i]>-1){ root = ds[i]; while(root>-1){ tmp = root; root = ds[root]; } break; } } out.print((char)('A'+tmp)); int flag=0; while(true){ flag=0; for(int i=0; i<26; i++){ if(tonari[tmp][i]){ out.print((char)('A'+i)); tmp = i; flag=1; } } if(flag==0) break; } out.println(); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }