在字符串 buffer/paragraph/text 中查找单词
Find word in string buffer/paragraph/text
这是在亚马逊电话面试中被问到的 - "Can you write a program (in your preferred language C/C++/etc.) to find a given word in a string buffer of big size ? i.e. number of occurrences "
我还在寻找我应该给面试官的完美答案。我试着写一个线性搜索(逐个字符比较),显然我被拒绝了。
给定 40-45 分钟的电话面试时间,he/she 寻找的完美算法是什么???
KMP 算法是一种流行的字符串匹配算法。
KMP Algorithm
按字符检查字符效率低下。如果字符串有 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
亚马逊和其他公司都希望了解 Boyer–Moore string search or / and Knuth–Morris–Pratt 算法。
如果你想表现出完美的知识,这些都很好。否则,尽量有创意,写一些相对优雅和高效的东西。
你在写任何东西之前问过分隔符吗?他们可能会简化您的任务以提供有关字符串缓冲区的一些额外信息。
如果您提前提供足够的信息,正确解释运行时、space 要求、数据容器的选择,即使下面的代码也可以(实际上不是)。
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];
}
这是在亚马逊电话面试中被问到的 - "Can you write a program (in your preferred language C/C++/etc.) to find a given word in a string buffer of big size ? i.e. number of occurrences "
我还在寻找我应该给面试官的完美答案。我试着写一个线性搜索(逐个字符比较),显然我被拒绝了。
给定 40-45 分钟的电话面试时间,he/she 寻找的完美算法是什么???
KMP 算法是一种流行的字符串匹配算法。
KMP Algorithm
按字符检查字符效率低下。如果字符串有 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
亚马逊和其他公司都希望了解 Boyer–Moore string search or / and Knuth–Morris–Pratt 算法。
如果你想表现出完美的知识,这些都很好。否则,尽量有创意,写一些相对优雅和高效的东西。
你在写任何东西之前问过分隔符吗?他们可能会简化您的任务以提供有关字符串缓冲区的一些额外信息。
如果您提前提供足够的信息,正确解释运行时、space 要求、数据容器的选择,即使下面的代码也可以(实际上不是)。
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];
}