Chinaunix
标题:
这道题用C语言怎么写,真是难倒我了
[打印本页]
作者:
justcoding
时间:
2015-10-18 21:51
标题:
这道题用C语言怎么写,真是难倒我了
今天参加某司的笔试,要求用C语言实现下面这道题:
把数据读入比较字符串string[],然后读入一个模式字符串,要求查找string[]中和模式字符串的所有匹配,输出行号、匹配字符串。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如"aa[123]bb", 就是说aa1bb, aa2bb, aa3bb都算匹配。
以下是实例,输入格式如下
4 //要输入的比较字符串行数
Aab //比较字符串
a2B
ab
ABB
a[a2b]b //模式字符串
输出如下
1 Aab
2 a2B
4 ABB
其中,1,2,4是输入时的比较字符串行号
对于这道题,我感觉就是加了中括号里的模式匹配不好解决,没想到好的办法,当时想了半天都不知所措,请大家给点意见
作者:
MMMIX
时间:
2015-10-18 22:32
回复
1#
justcoding
允许用正则库么?
作者:
MeRcy_PM
时间:
2015-10-18 23:26
如果只有简单的[]里面没其他表达式的话,简单想法直接开一条链表记录整个表达式,碰到[]这种就再建一个子链表把可能的值串起来。
struct {
struct list *next;
enum type;
union {
key;
list;
}
};
复制代码
或者用自动状态机吧
作者:
bruceteen
时间:
2015-10-19 08:35
随手写一个,不知道对不对
#include <stdbool.h>
#include <ctype.h>
bool match( const char* s, const char* t )
{
for( ; *s && *t; ++s,++t )
{
if( *t == '[' )
{
for( ++t; *t && *t!=']' && tolower(*s)!=tolower(*t); ++t );
if( *t=='\0' || *t==']' )
return false;
for( ; *t && *t!=']'; ++t );
}
else if( tolower(*s)!=tolower(*t) )
return false;
}
return !*s && !*t;
}
#include <stdio.h>
int main( void )
{
printf( "%s\n", match("Aab","a[a2b]b")?"true":"false" );
printf( "%s\n", match("a2B","a[a2b]b")?"true":"false" );
printf( "%s\n", match("ab","a[a2b]b")?"true":"false" );
printf( "%s\n", match("ABB","a[a2b]b")?"true":"false" );
}
复制代码
作者:
justcoding
时间:
2015-10-19 12:41
回复
2#
MMMIX
这个我倒没问,而且不是你说我还不知道有正则库这个东西,汗
作者:
captivated
时间:
2015-10-25 19:58
回复
5#
justcoding
这个东西, 如果用正则库确实不难. 如果不用正则库, 就相当于自己写正则库中的部分实现了.
考虑到是一个题目的话, 实际上应该是看楼主知不知道正则库这回事了... 搜索C语言正则库即可...
如果是要求正则全匹配而且不准用正则库的话, HoHo, 就相当于写一个正则库了, 这工作量... 不然呢, flex 什么的也是一个选择吧...
作者:
captivated
时间:
2015-10-25 20:02
仔细看了下好像是只要求匹配中括号, 那不用正则库也是可以处理的... 不过确实还是有些麻烦, 粗略写来估计 bug 不少.
作者:
yjh777
时间:
2015-10-27 15:54
fnmatch(3)
作者:
yjh777
时间:
2015-10-27 16:02
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* From FreeBSD fnmatch.c 1.11
* $Id: fnmatch.c,v 1.3 1997/08/19 02:34:30 jdp Exp $
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
#endif /* LIBC_SCCS and not lint */
/*
* Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
* Compares a filename or pathname to a pattern.
*/
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "fnmatch.h"
#define EOS '\0'
static const char *rangematch(const char *, char, int);
int
fnmatch(const char *pattern, const char *string, int flags)
{
const char *stringstart;
char c, test;
for (stringstart = string;;)
switch (c = *pattern++) {
case EOS:
if ((flags & FNM_LEADING_DIR) && *string == '/')
return (0);
return (*string == EOS ? 0 : FNM_NOMATCH);
case '?':
if (*string == EOS)
return (FNM_NOMATCH);
if (*string == '/' && (flags & FNM_PATHNAME))
return (FNM_NOMATCH);
if (*string == '.' && (flags & FNM_PERIOD) &&
(string == stringstart ||
((flags & FNM_PATHNAME) && *(string - 1) == '/')))
return (FNM_NOMATCH);
++string;
break;
case '*':
c = *pattern;
/* Collapse multiple stars. */
while (c == '*')
c = *++pattern;
if (*string == '.' && (flags & FNM_PERIOD) &&
(string == stringstart ||
((flags & FNM_PATHNAME) && *(string - 1) == '/')))
return (FNM_NOMATCH);
/* Optimize for pattern with * at end or before /. */
if (c == EOS)
if (flags & FNM_PATHNAME)
return ((flags & FNM_LEADING_DIR) ||
strchr(string, '/') == NULL ?
0 : FNM_NOMATCH);
else
return (0);
else if (c == '/' && flags & FNM_PATHNAME) {
if ((string = strchr(string, '/')) == NULL)
return (FNM_NOMATCH);
break;
}
/* General case, use recursion. */
while ((test = *string) != EOS) {
if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
return (0);
if (test == '/' && flags & FNM_PATHNAME)
break;
++string;
}
return (FNM_NOMATCH);
case '[':
if (*string == EOS)
return (FNM_NOMATCH);
if (*string == '/' && flags & FNM_PATHNAME)
return (FNM_NOMATCH);
if ((pattern =
rangematch(pattern, *string, flags)) == NULL)
return (FNM_NOMATCH);
++string;
break;
case '\\':
if (!(flags & FNM_NOESCAPE)) {
if ((c = *pattern++) == EOS) {
c = '\\';
--pattern;
}
}
/* FALLTHROUGH */
default:
if (c == *string)
;
else if ((flags & FNM_CASEFOLD) &&
(tolower((unsigned char)c) ==
tolower((unsigned char)*string)))
;
else if ((flags & FNM_PREFIX_DIRS) && *string == EOS &&
((c == '/' && string != stringstart) ||
(string == stringstart+1 && *stringstart == '/')))
return (0);
else
return (FNM_NOMATCH);
string++;
break;
}
/* NOTREACHED */
}
static const char *
rangematch(const char *pattern, char test, int flags)
{
int negate, ok;
char c, c2;
/*
* A bracket expression starting with an unquoted circumflex
* character produces unspecified results (IEEE 1003.2-1992,
* 3.13.2). This implementation treats it like '!', for
* consistency with the regular expression syntax.
* J.T. Conklin (conklin@ngai.kaleida.com)
*/
if ( (negate = (*pattern == '!' || *pattern == '^')) )
++pattern;
if (flags & FNM_CASEFOLD)
test = tolower((unsigned char)test);
for (ok = 0; (c = *pattern++) != ']';) {
if (c == '\\' && !(flags & FNM_NOESCAPE))
c = *pattern++;
if (c == EOS)
return (NULL);
if (flags & FNM_CASEFOLD)
c = tolower((unsigned char)c);
if (*pattern == '-'
&& (c2 = *(pattern+1)) != EOS && c2 != ']') {
pattern += 2;
if (c2 == '\\' && !(flags & FNM_NOESCAPE))
c2 = *pattern++;
if (c2 == EOS)
return (NULL);
if (flags & FNM_CASEFOLD)
c2 = tolower((unsigned char)c2);
if ((unsigned char)c <= (unsigned char)test &&
(unsigned char)test <= (unsigned char)c2)
ok = 1;
} else if (c == test)
ok = 1;
}
return (ok == negate ? NULL : pattern);
}
复制代码
http://web.mit.edu/freebsd/csup/fnmatch.c
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2