这道题属于字符匹配问题,同时也是寻找子链的一种思想,首先看完样例之后的第一个想法是对于"*"号的处理,应该如何下手。对于字符,最好的处理方法是for循环,同时,我们可以采用结构体储存的方法将开头和结尾分别储存起来,然后采用对比的方式,寻找符合的答案。
首先,介绍一种专门对比子链的函数:
函数名: strstr
函数原型:
1 |
|
语法:
1 |
|
str1: 被查找目标
str2: 要查找对象
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
来看一下题目:
Description
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 *.doc
Sample Output
command.com msdos.sys, io.sys, config.sys FILE NOT FOUND
中文:
描述
您是否曾使用过操作系统提供的文件搜索工具?例如,在DOS中,如果键入“dir * .exe”,操作系统将列出当前目录中扩展名为“exe”的所有可执行文件。这些天,你对你正在使用的糟糕的操作系统感到非常生气,你决定编写自己的操作系统。当然,您希望在操作系统中实现文件搜索功能。
输入
输入包含几个测试用例。连续的测试用例由空行分隔。每个测试用例都以整数N(1 <= N <= 100)开头,即当前目录中的文件数。然后是N行,每行有一个字符串,由小写字母('a'..'z')和点('。')组成,这是一个文件的名称。然后有一个整数M(1 <= M <= 20),查询次数。M行跟随,每个都有一个由小写字母组成的查询字符串,仅包含点和星号('*')。请注意,星号是“通用匹配字符”,用于表示不确定的零个或多个字符。一开始,你只想写一个简单的文件搜索版本,所以每个字符串包含不超过64个字符,并且查询字符串中只有一个星形字符。处理到文件结尾(EOF)。
产量
对于每个测试用例,为每个查询的结果生成一行。通过逗号(',')和空白('')字符在结果中分隔文件名。应根据输入中显示的顺序列出一个查询结果中的文件名。如果没有匹配的文件,则输出“FILE NOT FOUND”(不带引号)。用空行分隔两个连续的测试用例,但不要在最后一个之后输出额外的空白行。
来看一下代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
struct B
{
char first[110];
char last[110];
} c[110];
int main()
{
int n,i,k,m,s,j;
char a[110][110],b[110][110];
while(scanf("%d",&m)!=EOF)
{
for(i=0; i<=m-1; i++)
scanf("%s",a[i]);
scanf("%d",&n);
for(i=0; i<=n-1; i++)
{
s=0;
scanf("%s",b[i]);
for(j=0; b[i][j]!='*'; j++)
c[i].first[j]=b[i][j];
j++;
for(k=0; b[i][j]!='\0'; k++,j++)
c[i].last[k]=b[i][j];
if(strlen(c[i].first)+strlen(c[i].first)<strlen(a[i]))
for(k=0; k<=m-1; k++)
{
if(strstr(a[k],c[i].first)&&strstr(a[k],c[i].last))
{
if(s!=0)
printf(", ");
printf("%s",a[k]);
s++;
}
}
if(s!=0)
printf("\n");
}
if(s==0)
printf("FILE NOT FOUND\n");
printf("\n");
}
return 0;
}