备战华为机考4——字符串与正则表达式

正则表达式里面最常用的几个

[a-z] 

\\d{1}\\d?

[^a-z]

判断数组中是否存在次字符串

if(array[i].matches(res))

这里面知识太多了,还有pattern 和matcher

顶不住了,上一个刚写完的代码,看代码就能想起来。

补充一句 char转化为String String.valueOf(c);

import java.util.*;
import java.util.regex.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String str = scan.nextLine();
            if(str.length()<=8){
                System.out.println("NG");
                continue;
            }
            if(getMatch(str)){
                System.out.println("NG");
                continue;
            }
            if(getString(str, 0, 3)){
                System.out.println("NG");
                continue;
            }
            System.out.println("OK");
        }
        scan.close();
    }
    public static boolean getMatch(String str){
        int count = 0;
        Pattern p1 = Pattern.compile("[0-9]");
        if(p1.matcher(str).find()){
            count++;
        }
        Pattern p2 = Pattern.compile("[a-z]");
        if(p2.matcher(str).find()){
            count++;
        }
        Pattern p3 = Pattern.compile("[A-Z]");
        if(p3.matcher(str).find()){
            count++;
        }
        Pattern p4 = Pattern.compile("[^0-9a-zA-Z]");
        if(p4.matcher(str).find()){
            count++;
        }
        if(count>=3){
            return false;
        }
        else{
            return true;
        }
    }
    
    public static boolean getString(String str, int l, int r){
        if(r>str.length()){
            return false;
        }
        if(str.substring(r).contains(str.substring(l,r))){
            return true;
        }
        else{
            return getString(str,l+1,r+1);
        }
    }
}

 

再来补充一个,这个是华为机考J63,自己写出来的,用了数组内设置信号量判断substring的知识,比较经典。

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String str = scan.nextLine();
            int n = scan.nextInt();
            int max = 0;
            for(int i = 0; i<=str.length()-n; i++){
                if(str.substring(i,i+n).contains("C")||str.substring(i,i+n).contains("G")){
                    int count = 0;
                    for(char c : str.substring(i,i+n).toCharArray()){
                        if(c == 'C' || c == 'G'){
                            count++;
                        }
                    }
                    max = Math.max(max,count);
                }
            }
//             System.out.println(max);
            for(int i = 0; i<=str.length()-n; i++){
                if(str.substring(i,i+n).contains("C")||str.substring(i,i+n).contains("G")){
                    int sum = 0;
                    for(char c : str.substring(i,i+n).toCharArray()){
                        if(c == 'C' || c == 'G'){
                            sum++;
                        }
                    }
                    if(sum == max){
                        System.out.println(str.substring(i,i+n).toString());
                        break;
                    }
                }
            }
            
        }
        scan.close();
    }
}

 

posted @ 2022-03-26 18:52  曲海鑫  阅读(115)  评论(0)    收藏  举报