首页 > 试题广场 >

字符串加密

[编程题]字符串加密
  • 热度指数:140428 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS。如果单词中包含有重复的字母,只保留第1个,将所得结果作为新字母表开头,并将新建立的字母表中未出现的字母按照正常字母表顺序加入新字母表。如下所示:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

T R A I L B Z E S C D F G H J K M N O P Q U V W X Y (实际需建立小写字母的字母表,此字母表仅为方便演示)

上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。因此,使用这个密匙, Attack AT DAWN (黎明时攻击)就会被加密为Tpptad TP ITVH。

请实现下述接口,通过指定的密匙和明文得到密文。

数据范围: ,保证输入的字符串中仅包含小写字母


输入描述:

先输入key和要加密的字符串



输出描述:

返回加密后的字符串

示例1

输入

nihao
ni

输出

le
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] key = in.nextLine().toCharArray();
        char[] wolds = in.nextLine().toCharArray();
        Set<Character> set = new LinkedHashSet<>();
        for (char c : key) {
            set.add(c);
        }
        int k = 0;
        while (set.size() < 26) {
            char c = (char)('a' + k);
            set.add(c);
            k++;
        }
        Object[] arr = set.toArray();
        StringBuilder builder = new StringBuilder();
        for (char c : wolds) {
            int n = c - 'a';
            builder.append(arr[n]);
        }
        System.out.println(builder.toString());
    }
}

发表于 2024-03-09 19:19:55 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String key = in.nextLine();
        String mima = in.nextLine();
        //对key去重
        //LinkedHashSet保证顺序不打乱;
        Set<Character> keySet = new LinkedHashSet<>();
        for (int i = 0; i < key.length(); i++) {
            keySet.add(key.charAt(i));
        }

        StringBuilder sbKey = new StringBuilder();

        //创建一个字母表
        String ZhiMuBiao1 = "abcdefghijklmnopqrstuvwxyz";
        String ZhiMuBiao = "abcdefghijklmnopqrstuvwxyz";
        for (Character c : keySet) {
            ZhiMuBiao = ZhiMuBiao.replace(c + "", "");
            sbKey.append(c);
        }
        String newKeyBiao = sbKey.append(ZhiMuBiao).toString();

        char[] newBiao = newKeyBiao.toCharArray();

        char[] MiMaChar = mima.toCharArray();
        Map<Character, Character> maps = new HashMap<>();
        for (int i = 0; i < ZhiMuBiao1.length(); i++) {
            maps.put(ZhiMuBiao1.charAt(i), newBiao[i]);
        }
        for (int i = 0; i < MiMaChar.length; i++) {
            for (Character c : maps.keySet()) {
                if (MiMaChar[i] == c) {
                    System.out.print(maps.get(c));
                    break;
                }
            }
        }
        //abcdefghijklmnopqrstuvwxyz 相当于key
        //nihaobcdefgjklmpqrstuvwxyz    相当于value
    }
}
发表于 2024-03-06 13:50:09 回复(0)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Main {
    private static final int LOWER_CASE_SIZE = 26;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String key = null;

        while ((key = br.readLine()) != null) {
            char[] kChs = key.toLowerCase().toCharArray();
            char[] dict = new char[LOWER_CASE_SIZE];
            boolean[] seen = new boolean[LOWER_CASE_SIZE];
            int idx = 0;

            for (char ch : kChs) {
                if (seen[ch - 'a']) {
                    continue;
                }
                dict[idx++] = ch;
                seen[ch - 'a'] = true;
            }
            for (int i = 0; i < LOWER_CASE_SIZE; i++) {
                if (seen[i]) {
                    continue;
                }
                dict[idx++] = (char) (i + 'a');
            }
            
            char[] pChs = br.readLine().toLowerCase().toCharArray();
            StringBuilder sb = new StringBuilder();
            for (char ch : pChs) {
                sb.append(dict[ch - 'a']);
            }

            System.out.println(sb);
        }
    }
}

发表于 2024-03-05 10:34:30 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s1 =  in.nextLine();
            String s2 =  in.nextLine();
            Set<Character> tree = new LinkedHashSet();
            for (int i = 0; i < s1.length(); i++) {
                tree.add(s1.charAt(i));
            }
            //构建字母表
            String letter = "abcdefghijklmnopqrstuvwxyz";
            StringBuilder stringBuilder = new StringBuilder();
            for (Character c : tree) {
                letter = letter.replace(c+"","");
                stringBuilder.append(c);
            }
            stringBuilder.append(letter);
            //注意:字母字符的大小写状态应该保留
            StringBuilder res = new StringBuilder();
            for (char c : s2.toCharArray()) {
                if (Character.isUpperCase(c)){
                    res.append(Character.toUpperCase(stringBuilder.charAt(c - 'A')));
                }else if (Character.isLowerCase(c)){
                    res.append(stringBuilder.charAt(c - 'a'));
                }else {
                    res.append(c);
                }
            }
            System.out.println(res);
        }
    }
}

编辑于 2024-01-04 15:32:11 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String key = in.nextLine() + "abcdefghijklmnopqrstuvwxyz";
        String word = in.nextLine();
        List<Character> list = new ArrayList();
        for(int i = 0; i < key.length(); i++){
            if(!list.contains(key.charAt(i)))
                list.add(key.charAt(i));
        }
        for(int i = 0; i < word.length(); i++){
            System.out.print(list.get(word.charAt(i) - 'a'));
        }
    }
}

发表于 2023-11-23 22:33:50 回复(0)
import java.util.*;
import java.util.stream.Collectors;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String key = in.nextLine();
        String word = in.nextLine();

        String result = key + "abcdefghijklmnopqrstuvwxyz";
        List<Character> allList = new ArrayList<>();
        for(char c:result.toCharArray()){
            allList.add(c);
        }
        List<Character> list = allList.stream().distinct().collect(Collectors.toList());
        StringBuilder sb = new StringBuilder();
        for(char a:word.toCharArray()){
            sb.append(list.get(a-'a'));
        }
        System.out.println(sb.toString());
    }
}

发表于 2023-10-29 21:12:46 回复(1)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String key = in.nextLine();
        String input = in.nextLine();
        System.out.print(new Main().encrypt(key, input));
    }

    public String encrypt(String key, String input) {
        char[] chars = key.toCharArray();
        LinkedHashSet<Character> set = new LinkedHashSet<>();
        for (char ch : chars) {
            set.add(ch);
        }
        LinkedHashSet<Character> set1 = new LinkedHashSet<>();
        for (int i = 97; i <= 122; i++) {
            set1.add((char)i);
        }
        LinkedHashSet<Character> set2 = new LinkedHashSet<>();
        set2.addAll(set1);
        set1.removeAll(set);
        set.addAll(set1);

        HashMap<Character, Character> map = new HashMap<>();
        Iterator<Character> iterator2 = set2.iterator();
        Iterator<Character> iterator = set.iterator();

        for (int i = 0; i < set.size(); i++) {
            map.put(iterator2.next(), iterator.next());
        }

        char[] chs = input.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            if (Character.isLetter(chs[i])) {
                chs[i] = map.get(chs[i]);
            }
        }
        return new String(chs);
    }
}

发表于 2023-09-19 22:45:49 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String key = br.readLine();
        String line = br.readLine();

        // 业务场景为存取有序,且不重复,所以用linkedHashSet数据结构
        LinkedHashSet<Character> set = new LinkedHashSet<>();
        for (int i = 0; i < key.length(); i++) {
            char c = key.charAt(i);
            // 统一转换为大写
            set.add(Character.toUpperCase(c));
        }


        // 添加其他的字母到密钥中
        for (char i = 'A'; i <= 'Z'; i++) {
            set.add(i);
        }

        // 转换为list,方便后续使用索引找到对应加密后的字符
        ArrayList<Character> list = new ArrayList<>();
        for (Character character : set) {
            list.add(character);
        }

        // 打印密钥
        // System.out.println(list);

        // 正常顺序的字符,保存大写,与密钥大小写一致
        ArrayList<Character> list2 = new ArrayList<>();
        for (char i = 'A'; i <= 'Z'; i++) {
            list2.add(i);
        }
        //System.out.println(list2);

        StringBuilder result = new StringBuilder();
        // 遍历要加密的字符串
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            // 获取在正常字符顺序中的索引值
            int index = list2.indexOf(Character.toUpperCase(c));
            // 获取加密后的字符
            Character character = list.get(index);
            // 大小写保持一致
            if (Character.isLowerCase(c)){
                result.append(Character.toLowerCase(character));
            }else if (Character.isUpperCase(c)){
                result.append(Character.toUpperCase(character));
            }
        }

        System.out.println(result);

    }
}

发表于 2023-08-09 09:54:47 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        String password = in.nextLine();
        Set<Character> set = new TreeSet<>();
        List<Character> list = new ArrayList<>();
        for(int i = 0 ; i < str.length(); i ++){
            if(set.add(str.charAt(i))){
                list.add(str.charAt(i));
            }
        }
        
        for(int a = 97; a <= 122 ; a ++){ // a-z 97 -122
            if(list.contains((char)(a))){
                continue;
            }else{
                list.add((char)(a));
            }
        }

        for(int i = 0 ; i < password.length() ; i++){
            System.out.print(list.get((int)(password.charAt(i)) - 97)); // a-z 97 -122
        }
    
    }
}

发表于 2023-06-09 15:15:03 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //密匙key
        String key = in.nextLine();
        //要加密的字符串
        String str = in.nextLine();
        char[] keys = key.toCharArray();
        //keys 去重
        Set<Character> set = new LinkedHashSet<>();
        for (char c : keys) {
            set.add(c);
        }
        //set转String类型,去掉[]空格和逗号
        String keySet = set.toString().replaceAll("[\\[\\]\\s,]", "");
        //定义小写字母
        String alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
        String[] st = alphabet.split(" ");
        Map<String, String> map = new HashMap<>();
        //循环keySet,保存到map中,其中map的key为 alphabet中的前 keySet.length() 个单词 ,value为keySet的每个单词
        for (int i = 0; i < keySet.length(); i++) {
            map.put(st[i], String.valueOf(keySet.charAt(i)));
        }
        //循环alphabet ,定义res = keySet.length()
        for (int i = 0, res = keySet.length(); i < st.length; i++) {
            //如果 map中不包含value为 st[i],则需要把当前st[i] 作为value存入map中,把st[res++]作为map的key,
            if (!map.containsValue(st[i])) {
                map.put(st[res++], st[i]);
            }
        }
        //加密后的字符串
        String result = "";
        //遍历要加密的字符串,是否在map的key中,如果在,则输出对应的value
        for (int j = 0; j < str.length(); j++) {
            String c = String.valueOf(str.charAt(j));
            if (map.containsKey(c)) {
                result += map.get(c);
            }
        }
        System.out.println(result);
    }
}

发表于 2023-05-30 15:02:36 回复(0)
看着麻烦,实际还好,也没有坑
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 构建字典顺序26个字母的字母表
        List<Character> old = new ArrayList<Character>();
        for (int i = 0 ; i < 26 ; i++) {
            old.add((char)('a' + i));
        }
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String b = in.nextLine();
            List<Character> newList = new ArrayList<Character>();
            // 构建新的字母表,先放入key字符串
            for (int i = 0 ; i < a.length() ; i++) {
                char c = a.charAt(i);
                if (!newList.contains(c)) {
                    newList.add(c);
                }
            }
            // 构建新的字母表,顺序放入其他字符串
            for (int i = 0 ; i < 26 ; i++) {
                char c = old.get(i);
                if (!newList.contains(c)) {
                    newList.add(c);
                }
            }
            // 输出加密后的字符串
            for (int i = 0 ; i < b.length() ; i++) {
                char c = b.charAt(i);
                int index = c - 'a';
                System.out.printf(String.valueOf(newList.get(index)));
            }
            System.out.printf("\n");
        }
    }
}


发表于 2023-05-26 16:57:03 回复(0)
public class Test01 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String key = in.nextLine();
            String str = in.nextLine();
            //根据秘钥初始化字母表
            Character[] chars = init(key);
            
            StringBuffer sb = new StringBuffer();
            //遍历字符串,根据字母表得到密文
            for (int i = 0; i < str.length(); i++) {
                sb.append(chars[str.charAt(i)-'a']);
            }
            System.out.println(sb.toString());
        }
    }
    public static Character[] init(String key){
        Character[] table = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
                'w','x',
                'y','z'};
        ArrayList<Character> list = new ArrayList<>();
        //遍历key,添加到list集合中
        for (int i = 0; i < key.length(); i++) {
            char c = key.charAt(i);
            if(key.indexOf(c)==i){
                //使用过的字母标记为‘0’
                table[c-'a']='0';
                list.add(c);
            }
        }
        //将剩余字母添加到集合中
        for (int i = 0; i < table.length; i++) {
            if(table[i]!='0'){
                list.add(table[i]);
            }
        }
        return list.toArray(new Character[list.size()]);
    }
}

发表于 2023-05-16 16:59:05 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        List<Character> dic = new ArrayList<>();
        char start = 'a';

        for (int i = 0; i < 26; i++) {
            dic.add(start);
            start++;
        }

        String st ;
        while ((st = bf.readLine()) != null) {
            List<Character> list = new ArrayList<>();
            for (String s : Arrays.stream(st.split("")).distinct().collect(
                        Collectors.toList())) {
                list.add(s.charAt(0));
            }

            for (Character s : list) {
                dic.remove(s);
            }
            dic.addAll(0, list);
//加密字典构造好,后用再次得到字符依次获取字典中对应索引位置的字符
            //即使用传入字符-'a'活得索引值
            //依次获取
            char[] res = bf.readLine().toCharArray();
            for (int i = 0; i < res.length; i++) {
                res[i] = dic.get(res[i] - 'a');
            }
            System.out.println(res);

        }
    }
}

发表于 2023-05-12 14:39:15 回复(0)
通过全部用例    运行时间43ms    占用内存10920KB

import java.util.Scanner;

import java.util.HashMap;
import java.util.Map;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static final String ZMB = "abcdefghijklmnopqrstuvwxyz";
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String mima = in.nextLine();
        String mingwen = in.nextLine();
        in.close();
       
        StringBuilder stringBuilder = new StringBuilder();
        Map<Character, String> map = new HashMap<>();
        char[] array = mima.toCharArray();
        for (char ch : array) {
            if(!map.containsKey(ch)){
                map.put(ch, null);
                stringBuilder.append(ch);
            }
        }
       
        char[] zmbArray = ZMB.toCharArray();
        for (char ch : zmbArray) {
            if(!map.containsKey(ch)){
                map.put(ch, null);
                stringBuilder.append(ch);
            }
        }
       
        String newZMB = stringBuilder.toString();
        StringBuilder stringBuilder2 = new StringBuilder();
        char[] mingwenArray = mingwen.toCharArray();
        for (char ch : mingwenArray) {
            int index = ZMB.indexOf(ch);
            stringBuilder2.append(newZMB.charAt(index));
        }
       
        System.out.println(stringBuilder2.toString());
    }
}
发表于 2023-05-05 17:21:42 回复(0)
方法:处理规则,获取加密字符串位置,输出
String a = in.next();

            String b = in.next();

        ArrayList<Character> list=new ArrayList<>();  //存放密码规则

        for(char p:a.toLowerCase().toCharArray())    //处理密码规则

        {

            if(!list.contains(p))

            {

               list.add(p);

            }

        }

        char[] abc=new char[26];

        for(int i=0;i<26;i++)

        {

            char po='a';

            po+=i;

            abc[i]=po;

        }

        for(char p:abc)

        {

            if(!list.contains(p))

            {

                list.add(p);

            }

        }

   ArrayList<Integer> num=new ArrayList<>();  //获取加密字母在加密表的位置

        for(char p:b.toLowerCase().toCharArray())

        {

            char po='a';

            int k=0;

            k=(int)p-(int)po;

            num.add(k);

        }

        int n=0;

        String result="";

        for(char p:b.toCharArray())   //处理并输出结果

        {

            if(p>=65&&p<=90)

            {

                p=list.get(num.get(n));

                p-=32;

            }

            else

            {

                p=list.get(num.get(n));

            }

            result=result+p;

            n++;

        }

           System.out.println(result);  

        }

    }

发表于 2023-04-24 09:59:43 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String key = in.next();
            String s = in.next();
            char[] letters = new char[26];
            Set<Character> set = new HashSet<Character>();
            int j=0;
            for(int i=0;i<key.length();i++){//把key按照规则添加到字母表的首位
                if(!set.contains(key.charAt(i))){
                    letters[j++] = key.charAt(i);
                    set.add(key.charAt(i));
                }
            }
            //将剩余字母添加到新字母表中
            char c = 'a';
            for(int i=0;i<26;i++){
                char newLetter = (char)(((int)c-0)+i);
                if(!set.contains(newLetter)){
                    if(j>25) break;
                    letters[j++] = newLetter;
                    set.add(newLetter);
                }
            }
            
            //加密字符串
            StringBuilder newStr = new StringBuilder();
            for(int i=0;i<s.length();i++){
                int index = s.charAt(i)-'a';
                if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){
                    newStr.append((char)((int)letters[index]-32));
                }
                else{
                    newStr.append(letters[index]);
                }
            }
            System.out.println(newStr.toString());
        }
    }
}

发表于 2023-03-31 16:59:55 回复(0)
把要加密的字符串中所有字符的下标先保存起来。 然后把钥匙直接拼在字母表前面。用一个list去添加。然后再通过下标去list里面取。
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static String dict = "abcdefghijklmnopqrstuvwxyz";
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String key = in.nextLine();
            String pwd = in.nextLine();
            // 记录要加密的字符在字母表中的下标
            List<Integer> indexList = new ArrayList<>(pwd.length());
            for (char c : pwd.toCharArray()) {
                indexList.add(dict.indexOf(c));
            }

            String ss = key + dict;
            
            // 加密后的字母表
            List<Character> list = new ArrayList<>(26);
            for (int i = 0; i < ss.length(); i++) {
                char c = ss.charAt(i);
                if (!list.contains(c)) {
                    list.add(c);
                }
            }
            for (int i = 0; i < indexList.size(); i++) {
                System.out.print(list.get(indexList.get(i)));
            }
            System.out.println();
        }
    }

    // a b c d e f g h i j k l m n o p q r s t u v w x y z
    // n i h a o b c d e f g j k l m p q r s t 
}

发表于 2023-03-18 20:57:20 回复(0)

充分利用LinkedHashSet 的特性

  • 重复数据不能添加
  • 添加顺序保持一致
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // 注意 hasNext 和 hasNextLine 的区别
    while (in.hasNext()) { // 注意 while 处理多个 case
      String a = in.nextLine();
      String b = in.nextLine();
      Set set = new LinkedHashSet();
      // 链表的特点 重复值不能添加 顺序不会变化
      for (char c : a.toCharArray()) {
        if (Character.isUpperCase(c)) {
          c = Character.toLowerCase(c);
        }
        set.add(c);
      }
      for (char i = 'a'; i <= 'z'; i++) {
        set.add(i);
      }
      List list = new ArrayList(set);
      for (char c : b.toCharArray()) {
        if (Character.isLetter(c)) {
          // 取出集合中对应的值
          if (Character.isUpperCase(c)) {
            c = Character.toLowerCase(c);
            char cc = (char)list.get(c - 'a');
            System.out.print(Character.toUpperCase(cc));
          } else {
            System.out.print(list.get(c - 'a'));
          }
        } else {
          System.out.print(c);
        }
      }
    }
  }
}
发表于 2023-03-08 11:24:45 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String key = sc.nextLine();
        // 利用set去重,同时LinkedHashSet又是有序的
        Set<Character> set = new LinkedHashSet<>();
        for (char c : key.toCharArray()) {
            set.add(c);
        }
        for (char c = 'a'; c <= 'z'; c++) {
            set.add(c);
        }
        String word = sc.nextLine();
        // set不支持通过index获取元素,所以这里转为list
        List<Character> list = new ArrayList<>(set);
        for (char c : word.toCharArray()) {
            System.out.print(list.get(c - 'a'));
        }
    }
}

发表于 2023-02-21 18:04:24 回复(1)