BNU 25589 ls【字符的分離與匹配——有待完善Orz】

鏈接:




E. ls

1000ms
1000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
Font Size:  

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 組字符,如果能夠像上面說的一樣和第一個字符匹配則輸出當前字符。 

思路:以 ‘*’ 分離模板字符並且記錄分離出的每一個子串。
            如果下面的字符合法,那麼必然包含了從模板中分離的每一個子串。
            一次按照順序查找是否包含了模板的子串
            最後再判斷首尾的情況。。。。。。
亂搞的!!!感覺我的代碼只是判斷字符合法的必要不充分條件,比賽時實在沒辦法了,交了下居然水過了。



總結:


對於 STL 的應用還是不熟悉,否則應該敲的更快的,要好好收集下相關資料了總結下才是。
然後對於分離字符好像也有個split 函數直接分離的,我居然不會用,太弱了。


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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章