結果

問題 No.517 壊れたアクセサリー
ユーザー yuya178yuya178
提出日時 2017-06-18 03:31:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 3,892 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 78,048 KB
実行使用メモリ 54,144 KB
最終ジャッジ日時 2024-04-08 21:13:30
合計ジャッジ時間 3,985 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
54,128 KB
testcase_01 AC 54 ms
54,128 KB
testcase_02 AC 54 ms
54,120 KB
testcase_03 AC 54 ms
54,128 KB
testcase_04 AC 54 ms
54,144 KB
testcase_05 AC 55 ms
54,112 KB
testcase_06 AC 54 ms
54,120 KB
testcase_07 AC 55 ms
52,176 KB
testcase_08 AC 56 ms
54,120 KB
testcase_09 AC 55 ms
54,116 KB
testcase_10 AC 54 ms
54,120 KB
testcase_11 AC 54 ms
54,124 KB
testcase_12 AC 55 ms
54,100 KB
testcase_13 AC 53 ms
54,116 KB
testcase_14 AC 54 ms
54,128 KB
testcase_15 AC 54 ms
54,104 KB
testcase_16 AC 54 ms
54,108 KB
testcase_17 AC 54 ms
54,128 KB
testcase_18 AC 55 ms
54,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());
        }

    }
}
0