鏈接:
E. ls
You are implementing an operating system, and now need to write a program to list files in a directory: 'ls'. You want the user to be able to list only files that match a given pattern that can include wildcards (*), for example *.c. A wildcard matches zero or more characters of any kind.
Input
The first line contains a string P, containing 1-100 characters 'a'-'z', '*' and '.'. This is the pattern. The second line contains an integer N, 1 <= N <= 100, which is the number of files in the directory. Then follows N lines containing the names of the files in the directory. Each line is a string containing 1-100 characters 'a'-'z' and '.'.
Output
The output shall consist of the filenames that match the pattern, P, each on its own line, in the same order that they were given as input.
Sample Input
Sample Input 1 *.* 4 main.c a.out readme yacc Sample Input 2 *a*a*a 4 aaa aaaaa aaaaax abababa
Sample Output
Sample Output 1 main.c a.out Sample Output 2 aaa aaaaa abababa
題意+思路:
題意:先給你一個字符中間包含若干個 '*'
'*'代表在這些字符之間的 '*' 可以填充 0個或多個任意的字符
然後給你一個數 N
下面給你 N 組字符,如果能夠像上面說的一樣和第一個字符匹配則輸出當前字符。
總結:
code:
/*
字符的匹配,水過
題意:先給你一個字符中間包含若干個 '*'
'*'代表在這些字符之間的 '*' 可以填充 0個或多個任意的字符
然後給你一個數 N
下面給你 N 組字符,如果能夠像上面說的一樣和第一個字符匹配則輸出當前字符。
**/
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 110;
char s[maxn]; //第一個模板字符
string md[maxn]; //拆分第一個模板字符 '*'
string str;
string s2;
int n;
int main()
{
while(scanf("%s", &s) != EOF)
{
scanf("%d", &n);
int len = strlen(s);
char st = s[0];
char en = s[len-1];
for(int i = 0; i < maxn; i++) md[i] = "";
int num = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '*' || i == 0)
{
num++;
//i++;
while(s[i] == '*') i++; //排除多個 * 的情況
while(s[i] != '*' && i < len)
{
md[num]+=s[i];
i++;
}
i--;
}
}
//cout<<num<<endl;
if(md[num] == "") num--;
/*
cout<<num<<endl;
for(int i = 1; i <= num; i++)
cout<<md[i]<<endl;
*/
while(n--)
{
int flag = 1;
cin>>str;
int len1 = str.size();
int a = 0;
for(int i = 1; i <= num; i++)
{
string str1 = md[i];
string str2 = str.substr(a, len1);
if(str2.find(str1) == -1)
{
flag = 0; break;
}
a += str1.size();
}
if(flag) //如果包含了模板中的所有的,再比較首尾狀況
{
if(st == '*' && en == '*') cout<<str<<endl;
if(st == '*' && en != '*')
{
if(str[len1-1] == en) cout<<str<<endl;
}
if(st != '*' && en == '*')
{
if(str[0] == st) cout<<str<<endl;
}
if(st != '*' && en != '*')
{
if(str[0] == st && str[len1-1] == en) cout<<str<<endl;
}
}
}
}
return 0;
}