hdu 2279字符串处理

这题用Java最方便了,直接用Java的正则表达式匹配功能~~~~~~~

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNextInt()) {
            int N = cin.nextInt();
            int M = cin.nextInt();
            String[] strs = new String[N];
            for(int i = 0; i < N; i++) {
                strs[i] = cin.next();
            }
            for(int i = 0; i < M; i++) {
                String op = cin.next();
                op = op.replaceAll("\\?", Matcher.quoteReplacement("."));
                op = op.replaceAll("\\*", Matcher.quoteReplacement(".*"));
                Pattern p = Pattern.compile(op);
                int ans = 0;
                for(int j = 0; j < N; j++) {
                    if(p.matcher(strs[j]).matches()) {
                        ans++;
                    }
                }
                if(ans > 0) {
                    System.out.println(ans);
                }else {
                    System.out.println("Not match");
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/moonbay/p/2751872.html