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");
                }
            }
        }
    }
}
posted @ 2012-11-02 21:15  moonbay  阅读(116)  评论(0编辑  收藏  举报