拼多多笔试第二道,大佬来看看为什么错

思路感觉没问题,自测也没问题,但是就死活0%,是不是格式哪里有问题
就是回溯加剪枝
// 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int epoch = in.nextInt();
        String[][] input = new String[epoch][2];
        for(int round = 0;round < epoch;round++){
            input[round][0] = in.next();
            input[round][1] = in.next();
        }
        String out = "";

        for(int round = 0;round < epoch;round++){
//            String source_string = in.next();
//            String target_string = in.next();
            String source_string = input[round][0];
            String target_string = input[round][1];
            char[] source_array = source_string.toCharArray();
            ArrayList<char[]> result_array = new ArrayList<>();
            char[] method = new char[source_array.length];
            for(int i = 0;i < method.length;i++){
                method[i] = 'd';
            }
            helper(source_array, 0, "", method, target_string, result_array);
            String output = "{\n";
            for(char[] arr:result_array){
                for(int i = 0;i < arr.length;i++){
                    output += arr[i] + " ";
                }
                output += "\n";
            }
            if(round == epoch - 1){
                output += "}";
            }else{
                output += "}\n";

            }
            out += output;
        }
        System.out.println(out);
    }

    private static void helper(char[] source_array, int source_index, String result_tmp, char[] method, String target_string, ArrayList<char[]> result_array) {
        if (result_tmp.equals(target_string)) {
            result_array.add(method.clone());
            return;
        }
        if (source_index >= source_array.length) {
            return;
        }
        if (!target_string.contains(result_tmp)){
            return;
        }
        char current = source_array[source_index];
        method[source_index] = 'd';
        helper(source_array, source_index + 1, result_tmp, method, target_string, result_array);
        method[source_index] = 'l';
        helper(source_array, source_index + 1, current + result_tmp, method, target_string, result_array);
        method[source_index] = 'r';
        helper(source_array, source_index + 1, result_tmp + current, method, target_string, result_array);
    }
}


#拼多多##笔试题目#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务