题解 | #字符串加密#

字符串加密

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

import java.util.Scanner;

// 注意类名必须为 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 str = in.nextLine();
            String res = in.nextLine();
		  //存放新顺序的26个字母
            char [] arr  = new char[26];
		  //检验字母重复
            int [] check = new int [26];
            int j = 0;
		  //将key去重放入 arr中
            for (int i = 0 ; i < str.length(); i++) {
                char c = str.charAt(i);
                if (check[c - 'a'] == 0) {
                    arr[j] = c;
                    j++;
                }
                check[c - 'a'] = 1;
            }
		//将剩余的字母按顺序放入arr中
            for(int i = 0 ; i <26 ; i++){
                if(check[i]==0){
                    arr[j] = (char)('a'+i);
                    j++;
                }
            }
            StringBuffer resBuffer = new StringBuffer();
		  //解密
            for(int i = 0 ; i <res.length();i++){
              char c =   arr[res.charAt(i)-'a'];
              resBuffer.append(c);

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

全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:55
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务