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
如果只有简单的[]里面没其他表达式的话,简单想法直接开一条链表记录整个表达式,碰到[]这种就再建一个子链表把可能的值串起来。
  1. struct {
  2.     struct list *next;
  3.     enum type;
  4.     union {
  5.         key;
  6.         list;
  7.     }
  8. };
复制代码
或者用自动状态机吧
作者: bruceteen    时间: 2015-10-19 08:35
随手写一个,不知道对不对
  1. #include <stdbool.h>
  2. #include <ctype.h>

  3. bool match( const char* s, const char* t )
  4. {
  5.     for( ; *s && *t; ++s,++t )
  6.     {
  7.         if( *t == '[' )
  8.         {
  9.             for( ++t; *t && *t!=']' && tolower(*s)!=tolower(*t); ++t );

  10.             if( *t=='\0' || *t==']' )
  11.                 return false;

  12.             for( ; *t && *t!=']'; ++t );
  13.         }
  14.         else if( tolower(*s)!=tolower(*t) )
  15.             return false;
  16.     }

  17.     return !*s && !*t;
  18. }

  19. #include <stdio.h>

  20. int main( void )
  21. {
  22.     printf( "%s\n", match("Aab","a[a2b]b")?"true":"false" );
  23.     printf( "%s\n", match("a2B","a[a2b]b")?"true":"false" );
  24.     printf( "%s\n", match("ab","a[a2b]b")?"true":"false" );
  25.     printf( "%s\n", match("ABB","a[a2b]b")?"true":"false" );
  26. }
复制代码

作者: 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
  1. /*
  2. * Copyright (c) 1989, 1993, 1994
  3. *        The Regents of the University of California.  All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Guido van Rossum.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. *    notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. *    notice, this list of conditions and the following disclaimer in the
  15. *    documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. *    must display the following acknowledgement:
  18. *        This product includes software developed by the University of
  19. *        California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. *    may be used to endorse or promote products derived from this software
  22. *    without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * From FreeBSD fnmatch.c 1.11
  37. * $Id: fnmatch.c,v 1.3 1997/08/19 02:34:30 jdp Exp $
  38. */

  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. static char sccsid[] = "@(#)fnmatch.c        8.2 (Berkeley) 4/16/94";
  41. #endif /* LIBC_SCCS and not lint */

  42. /*
  43. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  44. * Compares a filename or pathname to a pattern.
  45. */

  46. #include <ctype.h>
  47. #include <string.h>
  48. #include <stdio.h>

  49. #include "fnmatch.h"

  50. #define        EOS        '\0'

  51. static const char *rangematch(const char *, char, int);

  52. int
  53. fnmatch(const char *pattern, const char *string, int flags)
  54. {
  55.         const char *stringstart;
  56.         char c, test;

  57.         for (stringstart = string;;)
  58.                 switch (c = *pattern++) {
  59.                 case EOS:
  60.                         if ((flags & FNM_LEADING_DIR) && *string == '/')
  61.                                 return (0);
  62.                         return (*string == EOS ? 0 : FNM_NOMATCH);
  63.                 case '?':
  64.                         if (*string == EOS)
  65.                                 return (FNM_NOMATCH);
  66.                         if (*string == '/' && (flags & FNM_PATHNAME))
  67.                                 return (FNM_NOMATCH);
  68.                         if (*string == '.' && (flags & FNM_PERIOD) &&
  69.                             (string == stringstart ||
  70.                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  71.                                 return (FNM_NOMATCH);
  72.                         ++string;
  73.                         break;
  74.                 case '*':
  75.                         c = *pattern;
  76.                         /* Collapse multiple stars. */
  77.                         while (c == '*')
  78.                                 c = *++pattern;

  79.                         if (*string == '.' && (flags & FNM_PERIOD) &&
  80.                             (string == stringstart ||
  81.                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  82.                                 return (FNM_NOMATCH);

  83.                         /* Optimize for pattern with * at end or before /. */
  84.                         if (c == EOS)
  85.                                 if (flags & FNM_PATHNAME)
  86.                                         return ((flags & FNM_LEADING_DIR) ||
  87.                                             strchr(string, '/') == NULL ?
  88.                                             0 : FNM_NOMATCH);
  89.                                 else
  90.                                         return (0);
  91.                         else if (c == '/' && flags & FNM_PATHNAME) {
  92.                                 if ((string = strchr(string, '/')) == NULL)
  93.                                         return (FNM_NOMATCH);
  94.                                 break;
  95.                         }

  96.                         /* General case, use recursion. */
  97.                         while ((test = *string) != EOS) {
  98.                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  99.                                         return (0);
  100.                                 if (test == '/' && flags & FNM_PATHNAME)
  101.                                         break;
  102.                                 ++string;
  103.                         }
  104.                         return (FNM_NOMATCH);
  105.                 case '[':
  106.                         if (*string == EOS)
  107.                                 return (FNM_NOMATCH);
  108.                         if (*string == '/' && flags & FNM_PATHNAME)
  109.                                 return (FNM_NOMATCH);
  110.                         if ((pattern =
  111.                             rangematch(pattern, *string, flags)) == NULL)
  112.                                 return (FNM_NOMATCH);
  113.                         ++string;
  114.                         break;
  115.                 case '\\':
  116.                         if (!(flags & FNM_NOESCAPE)) {
  117.                                 if ((c = *pattern++) == EOS) {
  118.                                         c = '\\';
  119.                                         --pattern;
  120.                                 }
  121.                         }
  122.                         /* FALLTHROUGH */
  123.                 default:
  124.                         if (c == *string)
  125.                                 ;
  126.                         else if ((flags & FNM_CASEFOLD) &&
  127.                                  (tolower((unsigned char)c) ==
  128.                                   tolower((unsigned char)*string)))
  129.                                 ;
  130.                         else if ((flags & FNM_PREFIX_DIRS) && *string == EOS &&
  131.                              ((c == '/' && string != stringstart) ||
  132.                              (string == stringstart+1 && *stringstart == '/')))
  133.                                 return (0);
  134.                         else
  135.                                 return (FNM_NOMATCH);
  136.                         string++;
  137.                         break;
  138.                 }
  139.         /* NOTREACHED */
  140. }

  141. static const char *
  142. rangematch(const char *pattern, char test, int flags)
  143. {
  144.         int negate, ok;
  145.         char c, c2;

  146.         /*
  147.          * A bracket expression starting with an unquoted circumflex
  148.          * character produces unspecified results (IEEE 1003.2-1992,
  149.          * 3.13.2).  This implementation treats it like '!', for
  150.          * consistency with the regular expression syntax.
  151.          * J.T. Conklin (conklin@ngai.kaleida.com)
  152.          */
  153.         if ( (negate = (*pattern == '!' || *pattern == '^')) )
  154.                 ++pattern;

  155.         if (flags & FNM_CASEFOLD)
  156.                 test = tolower((unsigned char)test);

  157.         for (ok = 0; (c = *pattern++) != ']';) {
  158.                 if (c == '\\' && !(flags & FNM_NOESCAPE))
  159.                         c = *pattern++;
  160.                 if (c == EOS)
  161.                         return (NULL);

  162.                 if (flags & FNM_CASEFOLD)
  163.                         c = tolower((unsigned char)c);

  164.                 if (*pattern == '-'
  165.                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  166.                         pattern += 2;
  167.                         if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  168.                                 c2 = *pattern++;
  169.                         if (c2 == EOS)
  170.                                 return (NULL);

  171.                         if (flags & FNM_CASEFOLD)
  172.                                 c2 = tolower((unsigned char)c2);

  173.                         if ((unsigned char)c <= (unsigned char)test &&
  174.                             (unsigned char)test <= (unsigned char)c2)
  175.                                 ok = 1;
  176.                 } else if (c == test)
  177.                         ok = 1;
  178.         }
  179.         return (ok == negate ? NULL : pattern);
  180. }
复制代码
http://web.mit.edu/freebsd/csup/fnmatch.c




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2