【笔试】敏感词替换-携程旅游2020秋招笔试第一场

时间:2020-9-9 作者:admin


描述

(匹配条件全排列)
输入三行
替换字符串
被替换部分,根据全排列进行匹配

解题思路

import java.util.*;

/**
 * @Author bennyrhys
 * @Date 2020-09-08 19:44
 * jack
 * i love jack,jack love me
 * you
 * i love you,you love me
 */
public class T42 {
    private static StringBuilder sb =  new StringBuilder();
    private static HashSet<String> hashSet = new HashSet<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] carr1 = sc.nextLine().toCharArray();
        char[] carr2 = sc.nextLine().toCharArray();
        char[] carr3 = sc.nextLine().toCharArray();

        /*for (char c : carr1) {
            System.out.print(c);
        }

        System.out.println();
        for (char c : carr2) {
            System.out.print(c);
        }
        System.out.println();
        for (char c : carr3) {
            System.out.print(c);
        }*/

        fullSort(carr1, 0, carr1.length-1);

//        System.out.println(sb.toString());

//        String[] qarr = sb.toString().split("*");
//        for (String s : qarr) {
//            System.out.println(s);
//            hashSet.add(s);
//        }
//
//        for (String s : hashSet) {
//            System.out.println(s);
//        }


        String res = "";

        String s = "";
        int len1 = carr1.length;
        int index = 0;
        for (int i = 0; i < carr2.length - len1 + 1; i++) {

             s = String.valueOf(carr2).substring(i, i+len1);
            //System.out.println(s);
            if (hashSet.contains(s)) {
//                System.out.println(s+"===");
                /*int flag = 0;
                while (flag < carr3.length) {
                    System.out.println(carr3[flag]);
                    carr2[i] = carr3[flag++];
                }*/
                res += String.valueOf(carr3);
                i = i + len1-1;
            }else {
//                res += String.valueOf(carr2[i]);
                res += carr2[i];
                index = i;
                // System.out.println(carr2[i]);
            }

        }
        if (index < carr2.length) {
            res += String.valueOf(carr2).substring(index+1, carr2.length);
        }



            System.out.print(res);
            //jack
            //i love jack,jack love me
            //you


    }
    // 全排列
    private static void fullSort(char[] carr , int l, int r) {
        if (l == r) {
           /* for (char c : carr) {
                sb.append(c);
                //System.out.println(c);
            }
            sb.append('*');*/
            //System.out.println();
//            System.out.println(String.valueOf(carr));
            hashSet.add(String.valueOf(carr));
        }
        for (int i = l; i <= r; i++) {
            swap(carr, i, l);
            fullSort(carr, l+1, r);
            swap(carr, l, i);
        }
    }

    // 交换
    private static void swap(char[] carr, int i, int l) {
        char temp = carr[i];
        carr[i] = carr[l];
        carr[l] = temp;
    }
}

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。