正则表达式

一种字符串压缩表示的解压

给几个用例:3bb,bbb,3b4b

import java.util.Scanner;
import java.util.*;
import java.util.regex.*;;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        if (Character.isDigit(s.charAt(s.length() - 1))){   //  最后一位是数字
            System.out.println("!error");
            return;
        }
        char[] chars = s.toCharArray();
        for (char c : chars) {
            //  不是 数字,也不是字母
           if (!Character.isDigit(c) && !Character.isLowerCase(c)){
                System.out.println("!error");
                return;
            }
        }

        Map<Integer, String> map = new HashMap<>();
        //  正则!!!
        String reg = "(\\d+)([a-zA-Z])";
        Matcher matcher = Pattern.compile(reg).matcher(s);
        while (matcher.find()){
            StringBuilder sb = new StringBuilder();
            int start = matcher.start();
            int count = Integer.parseInt(matcher.group(1));
            if (count > 100){  //  p1r2831z38268783t9o 题中说了:字符串的长度不会超过 100
                System.out.println("!error");
                return;
            }

            for (int i = 0; i < count; i++) {
                sb.append(matcher.group(2));
            }
            map.put(start, sb.toString());
        }


        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(i)){
                sb.append(map.get(i));
                int count = 0;
                int len = map.get(i).length();
                while (len > 0){
                    count++;
                    len /= 10;
                }
                i = i + count;
                continue;
            }
            sb.append(chars[i]);
        }
        if (isSame(sb.toString(), s)){
            System.out.println(sb.toString());
            return;
        }
        System.out.println("!error");
    }
    public static boolean isSame(String str, String target){
        Map<Integer, String> map = new HashMap<>();
        String reg = "([a-zA-Z])\\1{2,}";
        Matcher matcher = Pattern.compile(reg).matcher(str);
        while (matcher.find()){
            int start = matcher.start();
            String group = matcher.group();
            map.put(start, group);
        }
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(i)){
                sb.append(map.get(i).length());
                sb.append(chars[i]);
                i = i + map.get(i).length() - 1;
                continue;
            }
            sb.append(chars[i]);
        }
        return sb.toString().equals(target);
    }
}

OD274:最长公共后缀

import java.util.Scanner;
import java.util.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = new StringBuilder(in.nextLine()).reverse().toString().replaceAll("\"", "");
        int count = 0;
        String[] split = s.substring(1, s.length() - 1).split(",");
        int min = Integer.MAX_VALUE;
        for (String s1 : split) {
            min = Math.min(min, s1.length());
        }
        Set<String> set = new HashSet<>();
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < min; i++) {
            String temp = "";
            for (int j = 0; j < split.length; j++) {
                temp = split[j].charAt(i)+"";   //  有实力就留下来
                set.add(temp+"");
                if (set.size() > 1){  //  只要有一个不符合,就return了
                    if (res.length() == 0){
                        System.out.println("@Zero");  //  一个都没找到,返回特殊值
                    }else {
                        System.out.println(res.reverse());
                    }
                    return;
                }
            }
            set.clear();
            res.append(temp);
        }
        System.out.println(res.reverse());
    }
}

149. 求字符串中所有整数的最小和

注意点:

  1. 没有数字
  2. 大数
  3. BigInteger.add()不会改变原数据
  4. 要引入 math 包

import java.util.Scanner;
import java.util.;
import java.util.regex.
;;
import java.math.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String reg = "-[0-9]+";
Matcher matcher = Pattern.compile(reg).matcher(s);
Map<Integer, String> map = new HashMap<>();
while (matcher.find()){
int start = matcher.start();
String str = matcher.group(0);
map.put(start, str); // 记录负数起始位置,和字符串
}

    StringBuilder sb = new StringBuilder();
    char[] chars = s.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (map.containsKey(i)){
            sb.append(map.get(i) + " ");
            i = i + map.get(i).length() - 1;
            continue;
        }
        if (Character.isDigit(chars[i])){
            sb.append(chars[i] + " ");
        }
    }
    
    if (sb.length() == 0){
        System.out.println(0);
        return;
    }

    BigInteger res = new BigInteger("0");
    String[] split = sb.toString().split(" ");
    for (String s1 : split) {
        res =  res.add(new BigInteger(s1));
    }
    System.out.println(res);
}

}

146. 敏感字段加密

import java.util.Scanner;
import java.util.*;
import java.util.regex.*;;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       Map<Integer, String> map = new HashMap<>(); //  记录引号相关字符位置

        Scanner in = new Scanner(System.in);
        int index = Integer.parseInt(in.nextLine());
        String s = in.nextLine();
        StringBuilder sb = new StringBuilder();
        String reg = "\"\"|\"[a-zA-Z0-9_]+\"";  //  注意空格不能随意些,否则会匹配上!!!
        Matcher matcher = Pattern.compile(reg).matcher(s);
        while (matcher.find()){
            int start = matcher.start();
            String s1 = matcher.group().replaceAll("_", ",");  //  1. 特殊处理
            map.put(start, s1);
        }

        String[] split = s.split("");
        for (int i = 0; i < split.length; i++) {
            if (map.containsKey(i)){
                sb.append(map.get(i) + "_");
                i = i + map.get(i).length() - 1;
                continue;
            }
            sb.append(split[i]);
        }
        String temp = sb.toString().replaceAll("[_]+", "_");
        if (temp.startsWith("_")){  //  处理用例:___111___,除掉首尾的 _
            temp = temp.substring(1);
        }
        if (temp.endsWith("_")){
            temp = temp.substring(0, temp.length() - 1);
        }

        StringBuilder res = new StringBuilder();
        String[] split1 = temp.split("_");
        if (index <= split1.length - 1){
            split1[index] = "******";
            for (String s1 : split1) {
                String replace = s1.replace(",", "_");  //  2. 还原
                res.append(replace + "_");
            }
            System.out.println(res.toString().substring(0, res.length() - 1));
            return;
        }
        System.out.println("ERROR");
    }
}

262. 计算误码率

import java.util.Scanner;
import java.util.*;
import java.util.regex.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str1 = getStr(in.nextLine());
        String str2 = getStr(in.nextLine());
        
        int len = str1.length();
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (str1.charAt(i) != str2.charAt(i)){
                count++;
            }
        }
        System.out.println(count + "/" + len);
    }
    public static String getStr(String str){
        String reg = "\\d+[^0-9]";  //  非数字字符
        StringBuilder sb = new StringBuilder();
        Matcher matcher = Pattern.compile(reg).matcher(str);
        while (matcher.find()){
            String group = matcher.group();
            int count = Integer.parseInt(group.substring(0, group.length() - 1));
            char c = group.charAt(group.length() - 1);
            for (int i = 0; i < count; i++) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

273. 增强的 strstr【⭐】

将括号内的所有东西都当作普通字符:

  1. 如果不是转义字符 \ 的话,那么就加上 2个 \即可,但是 Java 中,2个 \ ==> \,所以要在前面加上4个\
  2. 是转义字符 \ 的话 ==> \ 的转义为 \\:即要在前面加上3个\,所以 3 * 2 = 6个 \
import java.util.Scanner;
import java.util.*;

import java.util.regex.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String reg = in.nextLine();
        StringBuilder sb = new StringBuilder();
        char[] chars = reg.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String temp = reg.substring(i, i + 1);
            if ("[".equals(temp)){
                sb.append("[");
                while (!"]".equals(reg.substring(i + 1, i + 2))) {
                    if (!"\\".equals(reg.substring(i + 1, i + 2))) {  //  当前不是 \
                        sb.append("\\\\" + reg.substring(i + 1, i + 2));
                        i++;
                    }else {
                        sb.append("\\\\\\" + reg.substring(i + 1, i + 2));
                        i++;
                    }
                }
                sb.append("]"); // 加上最后一个 ]
                i++;
            }else {
                sb.append(temp);
            }
        }
        Matcher matcher = Pattern.compile(sb.toString()).matcher(s);
        if (matcher.find()){
            System.out.println(matcher.start());
            return;
        }
        System.out.println("-1");
    }
}
posted @ 2023-09-05 00:17  爱新觉罗LQ  阅读(2)  评论(0编辑  收藏  举报