题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String dan = scanner.nextLine();
            String juz = scanner.nextLine();
//            System.out.println(dan);
//            System.out.println(juz);
            LinkedHashMap<String, String> gt = Main.getalphabet(dan);
            String jiami = Main.jiami(juz, gt);
            System.out.println(jiami);
        }
    }
    public static LinkedHashMap<String,String> getalphabet(String s){
        String[] split = s.split("");
        LinkedHashSet<String> hs = new LinkedHashSet<>();
        String si="";
        for (int i = 0; i < split.length; i++) {
            if (hs.contains(split[i])){
                continue;
            }else {
                hs.add(split[i]);
                si=si+split[i];
            }
        }
        String alp="abcdefghijklmnopqrstuvwxyz";
        LinkedHashSet<String> hss = new LinkedHashSet<>();
        String[] split1 = alp.split("");
        String sii="";
        for (int i = 0; i < split1.length; i++) {
            if (hs.contains(split1[i])){
                continue;
            }else {
                hss.add(split1[i]);
                sii=sii+split1[i];
            }
        }
        si=si+sii;
        LinkedHashMap<String, String> lhm = new LinkedHashMap<>();
        for (int i = 0; i <alp.length(); i++) {
            lhm.put(alp.substring(i,i+1),si.substring(i,i+1));
        }
        return lhm;
    }

    public static String jiami(String j,LinkedHashMap<String,String> lhm){
        String[] split = j.split(" ");
        for (int i = 0; i < split.length; i++) {
            String[] split1 = split[i].split("");
            String nu="";
            for (int k = 0; k <split1.length; k++) {
                split1[k] = split1[k].replace(split1[k], lhm.get(split1[k]));
                nu=nu+split1[k];
            }
            split[i]=nu;
        }
        String succss="";
        for (int i = 0; i < split.length; i++) {
            succss=succss+split[i];
        }

        return succss;
    }
}

全部评论

相关推荐

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