内存:64  时间:1

题目描述

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.

输入

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).

输出

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.

样例输入

4
command.com
msdos.sys
io.sys
config.sys
2
com*.com
*.sys

3
a.txt
b.txt
c.txt
1
*.doc

样例输出

command.com
msdos.sys, io.sys, config.sys

FILE NOT FOUND

提示

zoj2840

代码如下

#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 65
using namespace std;
char q[MAX];
char s[101][MAX];
int len[101];
int main()
{
    int n,m;
    int k1,k2,lenq;
    int i,j;
    bool first=true;
    bool f;
    while(cin>>n)
    {
        if(first)first=false;
        else cout<<endl;
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            len[i]=strlen(s[i]);
        }
        cin>>m;
        for(i=0;i<m;i++)
        {
            f=true;
            scanf("%s",q);
            lenq=strlen(q);
            for(j=0;j<n;j++)
            {
                k1=k2=0;
                if(lenq>len[j])continue;
                while(q[k1]==s[j][k2])k1++,k2++;
                if(q[k1]!='*')continue;
                k1=lenq-1,k2=len[j]-1;
                while(q[k1]==s[j][k2])k1--,k2--;
                if(q[k1]=='*')
                {
                    if(f)cout<<s[j],f=false;
                    else
                    cout<<", "<<s[j];
                }
            }
            if(f)cout<<"FILE NOT FOUND";
            cout<<endl;
        }
    }
    return 0;
}

代码来源于互联网,仅供参考!