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

链接:

http://www.bnuoj.com/bnuoj/problem_show.php?pid=25589

http://www.bnuoj.com/bnuoj/contest_show.php?cid=2321#problem/25866


E. ls

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Submit  Status  PID: 25589
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
Submit  Status  PID: 25589



题意+思路:


字符的匹配,水过
题意:先给你一个字符中间包含若干个 '*'
           '*'代表在这些字符之间的 '*' 可以填充 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;
}


你可能感兴趣的:(BNU 25589 ls【字符的分离与匹配——有待完善Orz】)