首页 > ACM题库 > HDU-杭电 > hdu 2279 File Search Tool[解题报告]java
2014
01-04

hdu 2279 File Search Tool[解题报告]java

File Search Tool

问题描述 :

WisKey downloaded much software in winter vacation, and the disk was in a state of confusion. He wastes many times to find the file everyday. So he wants a tool to help him do this work.
The file name consists of lowercase letters.
The name pattern is a string of lowercases, ‘?’s and ‘*’s. In a pattern, a ‘?’ matches any single lowercase, and a ‘*’ matches none or more lowercases.
Let’s do this~

输入:

The first line of input contains two integers N (0 < N <= 10000) and M (0 < M <=100), representing the number of file names and the number of word patterns.
Each of the following N lines contains a file name. After those, each of the last M lines contains a name pattern.
You can assume that the length of patterns will not exceed 6, and the length of file names will not exceed 20.
There are multiple cases in the data file, process to end of file.

输出:

The first line of input contains two integers N (0 < N <= 10000) and M (0 < M <=100), representing the number of file names and the number of word patterns.
Each of the following N lines contains a file name. After those, each of the last M lines contains a name pattern.
You can assume that the length of patterns will not exceed 6, and the length of file names will not exceed 20.
There are multiple cases in the data file, process to end of file.

样例输入:

4 5
this
the
an
is
t*
?h*s
??e*
*s
e

样例输出:

2
1
1
2
Not match

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext())
		{
		   int n=scanner.nextInt();
		   int m=scanner.nextInt();
		   String string[]=new String[n+1];
		   for(int i=0;i<n;i++)
		   {
			   string[i]=scanner.next();
		   }
		   for(int i=0;i<m;i++)
		   {
			   String string2=scanner.next();
			   String ss="^";
			   string2=string2.replace("?", ".");
			   string2=string2.replace("*", ".*");//.*表示0个或多个字母,因为“.”就表示一个字符
			   ss+=string2;
			   ss+="$";
			   int count=0;
			   Pattern pattern=Pattern.compile(ss);
			   for(int j=0;j<n;j++)
			   {
				   Matcher matcher=pattern.matcher(string[j]);
				   if(matcher.find())count++;
			   }
			   if(count==0)System.out.println("Not match");
			   else System.out.println(count);
		   }
		}
	}
}

解题转自:http://blog.csdn.net/taotaotaotao910429/article/details/8246369


  1. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])