結果
問題 | No.517 壊れたアクセサリー |
ユーザー | yuya178 |
提出日時 | 2017-06-18 03:31:22 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,885 bytes |
コンパイル時間 | 2,478 ms |
コンパイル使用メモリ | 78,072 KB |
実行使用メモリ | 52,340 KB |
最終ジャッジ日時 | 2024-10-01 12:13:22 |
合計ジャッジ時間 | 4,155 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 56 ms
50,444 KB |
testcase_02 | AC | 55 ms
50,220 KB |
testcase_03 | WA | - |
testcase_04 | AC | 57 ms
50,032 KB |
testcase_05 | WA | - |
testcase_06 | AC | 56 ms
50,204 KB |
testcase_07 | AC | 56 ms
50,268 KB |
testcase_08 | AC | 57 ms
50,380 KB |
testcase_09 | WA | - |
testcase_10 | AC | 57 ms
50,456 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 56 ms
50,508 KB |
testcase_17 | AC | 56 ms
50,320 KB |
testcase_18 | WA | - |
ソースコード
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; out.println(ds[2]); for(int i=0; i<26; i++){ if(ds[i]>-1){ root = ds[i]; while(root>-1){ tmp = root; root = ds[root]; } break; } } 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()); } } }