【问题标题】:Find word in string buffer/paragraph/text在字符串缓冲区/段落/文本中查找单词
【发布时间】:2016-06-01 20:57:58
【问题描述】:

这是在亚马逊电话采访中被问到的 - “你能写一个程序(用你喜欢的语言 C/C++/等)在一个大的字符串缓冲区中找到一个给定的单词吗?即数字出现次数“

我仍在寻找我应该给面试官的完美答案。我尝试编写线性搜索(逐字符比较),显然我被拒绝了。

给定 40-45 分钟的电话面试时间,他/她正在寻找的完美算法是什么???

【问题讨论】:

  • 也许可以问几个后续问题。 “你能告诉我那个缓冲区大小的数量级吗?” “如果我建立一个索引,它会在前期计算上很昂贵,但对于后续读取来说会非常快。这是适合这种情况的策略吗?”

标签: algorithm amazon


【解决方案1】:

KMP 算法是一种流行的字符串匹配算法。

KMP Algorithm

逐字符检查 char 效率低下。如果字符串有 1000 个字符,而关键字有 100 个字符,则您不想执行不必要的比较。 KMP 算法处理了许多可能发生的情况,但我想面试官正在寻找以下情况:当你开始(通过 1)时,前 99 个字符匹配,但第 100 个字符不匹配。现在,对于第 2 步,您不必从字符 2 执行整个比较,而是有足够的信息来推断下一个可能的匹配可以从哪里开始。

// C program for implementation of KMP pattern searching 
// algorithm
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void computeLPSArray(char *pat, int M, int *lps);

void KMPSearch(char *pat, char *txt)
{
int M = strlen(pat);
int N = strlen(txt);

// create lps[] that will hold the longest prefix suffix
// values for pattern
int *lps = (int *)malloc(sizeof(int)*M);
int j  = 0;  // index for pat[]

// Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps);

int i = 0;  // index for txt[]
while (i < N)
{
  if (pat[j] == txt[i])
  {
    j++;
    i++;
  }

  if (j == M)
  {
    printf("Found pattern at index %d \n", i-j);
    j = lps[j-1];
  }

  // mismatch after j matches
  else if (i < N && pat[j] != txt[i])
  {
    // Do not match lps[0..lps[j-1]] characters,
    // they will match anyway
    if (j != 0)
     j = lps[j-1];
    else
     i = i+1;
  }
}
free(lps); // to avoid memory leak
}

void computeLPSArray(char *pat, int M, int *lps)
{
int len = 0;  // length of the previous longest prefix suffix
int i;

lps[0] = 0; // lps[0] is always 0
i = 1;

// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
   if (pat[i] == pat[len])
   {
     len++;
     lps[i] = len;
     i++;
   }
   else // (pat[i] != pat[len])
   {
     if (len != 0)
     {
       // This is tricky. Consider the example 
       // AAACAAAA and i = 7.
       len = lps[len-1];

       // Also, note that we do not increment i here
     }
     else // if (len == 0)
     {
       lps[i] = 0;
       i++;
     }
   }
}
}

// Driver program to test above function
int main()
{
char *txt = "ABABDABACDABABCABAB";
char *pat = "ABABCABAB";
KMPSearch(pat, txt);
return 0;
}

这段代码取自一个非常好的教授算法的网站: Geeks for Geeks KMP

【讨论】:

  • 我的荣幸!我是算法的狂热分子,哈哈,我很高兴它有帮助。您能否“接受”我的回答,以便其他人知道它解决了您的问题?
【解决方案2】:

亚马逊和其他公司都希望了解 Boyer–Moore string search 或 / 和 Knuth–Morris–Pratt 算法。

如果您想展示完美的知识,这些都很好。否则,尝试有创意,写一些相对优雅和高效的东西。

你在写任何东西之前问过分隔符吗?它们可能会简化您的任务,以提供有关字符串缓冲区的一些额外信息。

如果您提前提供足够的信息,正确解释运行时、空间要求、数据容器的选择,即使下面的代码也可以(实际上不是)。

int find( std::string & the_word, std::string & text )
{
    std::stringstream ss( text );    // !!! could be really bad idea if 'text' is really big

    std::string word;
    std::unordered_map< std::string, int > umap;
    while( ss >> text ) ++umap[text];   // you have to assume that each word separated by white-spaces.
    return umap[the_word];
}

【讨论】:

  • 我无法将 cmets 添加到其他答案,但 OP 声明 即出现次数"
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2014-10-22
  • 2020-08-18
  • 2021-03-30
  • 1970-01-01
  • 2013-12-19
  • 2015-12-05
相关资源
最近更新 更多