結果
問題 | No.517 壊れたアクセサリー |
ユーザー | yuya178 |
提出日時 | 2017-06-18 03:23:11 |
言語 | Java21 (openjdk 21) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,891 bytes |
コンパイル時間 | 2,236 ms |
コンパイル使用メモリ | 78,024 KB |
実行使用メモリ | 50,568 KB |
最終ジャッジ日時 | 2024-10-01 12:13:16 |
合計ジャッジ時間 | 4,203 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
50,536 KB |
testcase_01 | AC | 51 ms
50,260 KB |
testcase_02 | AC | 50 ms
50,072 KB |
testcase_03 | AC | 52 ms
50,504 KB |
testcase_04 | AC | 54 ms
50,528 KB |
testcase_05 | RE | - |
testcase_06 | AC | 54 ms
50,388 KB |
testcase_07 | AC | 55 ms
50,288 KB |
testcase_08 | AC | 54 ms
50,356 KB |
testcase_09 | AC | 56 ms
50,520 KB |
testcase_10 | AC | 54 ms
50,108 KB |
testcase_11 | AC | 55 ms
50,536 KB |
testcase_12 | AC | 53 ms
50,036 KB |
testcase_13 | AC | 52 ms
50,568 KB |
testcase_14 | AC | 55 ms
50,476 KB |
testcase_15 | AC | 53 ms
50,516 KB |
testcase_16 | AC | 55 ms
50,352 KB |
testcase_17 | AC | 55 ms
50,448 KB |
testcase_18 | AC | 55 ms
50,400 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]>0){ 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()); } } }