ZOJ Problem Set - 2840 File Searching

来源:互联网 发布:实业难做 知乎 编辑:程序博客网 时间:2024/06/06 03:22
File Searching

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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:
4command.commsdos.sysio.sysconfig.sys2com*.com*.sys3a.txtb.txtc.txt1*.doc
Sample Output:
command.commsdos.sys, io.sys, config.sysFILE NOT FOUND

Author: ZHOU, Yuan
Source: Zhejiang University Local Contest 2007





分析:
题意:
给定若干待查询的字符串。再给出若干查询字符串,查询字符串的“*”可以代表0个或任意多个字符。要求输出所有符合条件的字符串。



算法简单,但是要求有扎实的编程能力。根据*对查询字符串进行处理,分成左右两部分。再分别比较左右字符串是否相等就可以。


ac代码:
#include <iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
string s,Left,Right;
vector<string> v,vv;
int main()
{
    int n,m,i,j;
    int c=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(c) cout<<endl;
        c++;
        v.clear();//清空
        for(i=0;i<n;i++)
        {
            cin>>s;
            v.push_back(s);//将待查询的字符串放进vector容器
        }
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            cin>>s;
            int p=s.find("*");//*作为左右的分界点
            Left="";//清空
            Right="";
            vv.clear();
            for(j=0;j<p;j++)//左边的字符放进左串
            Left+=s[j];
            for(j=p+1;j<s.size();j++)//右边的字符放进右串
            Right+=s[j];
            for(j=0;j<v.size();j++)
            {
                if((Left.size()+Right.size()>v[j].size()))//如果待查询的字符串比左串加右串的长度还短,说明不可能,不用继续比较。剪枝。
                continue;
                if(Left.size()!=0)//比较左串的是否相等
                {
                    if(v[j].find(Left)!=0)
                    continue;
                }
                if(Right.size()!=0)//反转右串后比较
                {
                    reverse(Right.begin(),Right.end());
                    reverse(v[j].begin(),v[j].end());
                    if(v[j].find(Right)!=0)
                    {
                         reverse(Right.begin(),Right.end());
                         reverse(v[j].begin(),v[j].end());
                         continue;
                    }
                    reverse(Right.begin(),Right.end());
                    reverse(v[j].begin(),v[j].end());
                }
                vv.push_back(v[j]);//如果左右都符合,放进vector容器
            }
            for(j=0;j<vv.size();j++)//输出所有符合条件的string
            {
                cout<<vv[j];
                if(j!=vv.size()-1)
                cout<<", ";
                else cout<<endl;
            }
            if(vv.size()==0)//如果vector容器为空,说明找不到。
            cout<<"FILE NOT FOUND"<<endl;
        }
    }
    return 0;
}
0 0
原创粉丝点击