题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1840
Have you ever used file searching tools provided by an operating system? For example, in DOS, if you type "dir *.exe", the OS will list all executable files with extension "exe" in the current directory. These days, you are so mad with the crappy operating system you are using and you decide to write an OS of your own. Of course, you want to implement file searching functionality in your OS.
Input:
The input contains several test cases. Consecutive test cases are separated by a blank line.
Each test case begins with an integer N (1 <= N < =100), the number of files in the current directory. Then N lines follow, each line has one string consisting of lowercase letters (‘a‘..‘z‘) and the dot (‘.‘) only, which is the name of a file. Then there is an integer M (1 <= M <= 20), the number of queries. M lines follow, each has one query string consisting of lowercase letters, the dot and the star (‘*‘) character only. Note that the star character is the "universal matching character" which is used to represent zero or numbers of characters that are uncertain. In the beginning, you just want to write a simple version of file searching, so every string contains no more than 64 characters and there is one and only one star character in the query string.
Process to the End Of File (EOF).
Output:
For each test case, generate one line for the results of each query. Separate file names in the result by a comma (‘,‘) and a blank (‘ ‘) character. The file names in the result of one query should be listed according to the order they appear in the input. If there is no matching file, output "FILE NOT FOUND" (without the quotation) instead.
Separate two consecutive test cases with a blank line, but Do NOT output an extra blank line after the last one.
Sample Input:4 command.com msdos.sys io.sys config.sys 2 com*.com *.sys 3 a.txt b.txt c.txt 1 *.docSample Output:
command.com msdos.sys, io.sys, config.sys FILE NOT FOUND
题意:
‘*’为通配符,求哪些能匹配!
代码如下:
#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; int main() { int n, m; int mark = 0; string s[117]; while(cin >> n) { if(mark) { printf("\n"); } mark = 1; for(int i = 0; i < n; i++) { cin >> s[i]; } cin >> m; int flag = 0; string a; for(int i = 0; i < m; i++) { cin >> a; flag = 0; for(int j = 0; j < n; j++) { string b = s[j]; int len1 = a.size(); int len2 = b.size(); if(len2+1 < len1) { continue; } int pos = 0; for(int k = 0; k < len1; k++)//寻找*号位置 { if(a[k] == '*') { pos = k; break; } } int flag1 = 0; for(int h = 0; h < pos; h++) { if(a[h] != b[h]) { flag1 = 1; break; } } if(flag1) { continue; } for(int f = 1; f < len1-pos; f++) { if(a[len1-f] != b[len2-f]) { flag1 = 1; break; } } if(flag1) { continue; } else { if(flag) { printf(", "); } cout<<b; // if(!flag) // { // printf("\n"); // } flag = 1; } } if(flag) { printf("\n"); } else { printf("FILE NOT FOUND\n"); } } } return 0; } /* 4 command.com msdos.sys io.sys config.sys 2 *.sys com*.com */
原文地址:http://blog.csdn.net/u012860063/article/details/45046925