• July面试整理系列--(3)


    题目描述:

      在一篇英文文章中查找指定的人名,人名使用二十六个英文字母(可以是大写或小写)、空格以及两个通配符组成(*、?),通配符“*”表示零个或多个任意字母,通配符“?”表示一个任意字母。
    如:“J* Smi??” 可以匹配“John Smith” .

    请用C语言实现如下函数:
    void scan(const char* pszText, const char* pszName);
    注:pszText为整个文章字符,pszName为要求匹配的英文名。
    请完成些函数实现输出所有匹配的英文名,除了printf外,不能用第三方的库函数等。

    下面使用c++实现的。

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <vector>
     7 #include <string>
     8 #include <climits>
     9 using namespace std;
    10 
    11 /*
    12 从文章的第一个字符开始往后匹配,匹配成功就输出,不成功就
    13 继续往后匹配,复杂度O(M*N)
    14 dfs
    15 可以考虑加上记忆化:bool DP[i][j]表示i到结尾,以及j到结尾,a,b是否匹配
    16 */
    17 int enda;
    18 bool match(string a,int sa,string b,int sb)
    19 {
    20     //匹配完成
    21     if(sb == b.length())
    22     {
    23         enda = sa;
    24         return true;
    25     }
    26     //未匹配完成
    27     if(sa == a.length())
    28     {
    29         if(b[sb] == '*')
    30         {
    31             enda = sa;
    32             return true;
    33         }
    34         return false;
    35     }
    36     if(b[sb] != '*' && b[sb] != '?')
    37     {
    38         if(a[sa] == b[sb])
    39             return match(a,sa+1,b,sb+1);
    40         return false;
    41     }
    42     else
    43     {
    44         if(b[sb] == '*')
    45         {
    46             return match(a,sa,b,sb+1)||match(a,sa+1,b,sb);
    47         }
    48         else
    49         {
    50             return match(a,sa+1,b,sb+1);
    51         }
    52     }
    53 }
    54 
    55 void scan(string a,string b)
    56 {
    57     int sa = 0;
    58     int sb = 0;
    59 
    60     int lena = a.length();
    61     int lenb = b.length();
    62 
    63     while(sa < lena)
    64     {
    65         if(match(a,sa,b,0))
    66         {
    67             cout<<a.substr(sa,enda-sa);
    68             return;
    69         }
    70         else
    71         {
    72             sa++;
    73         }
    74     }
    75     return;
    76 }
    77 
    78 int main()
    79 {
    80     string a = "J* Smi??";
    81     string b = "John Smith";
    82     scan(b,a);
    83     return 0;
    84 }
  • 相关阅读:
    程序员版孔乙己
    痛!痛!痛!我们的好兄弟Git,一路走好!
    谈谈中台架构之交易中台
    一个单例还能写出花来吗?
    为什么数据库字段要使用NOT NULL?
    阿里二面:什么是mmap?
    退税不完全操作指南,我这样操作省了2000块!
    开源组件编排引擎LiteFlow发布里程碑版本2.5.0
    开源框架TLog核心原理架构解析
    对乱糟糟的日志说再见
  • 原文地址:https://www.cnblogs.com/cane/p/3848724.html
Copyright © 2020-2023  润新知