題目
對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置(從0開始)。如果不存在,則返回-1
。
您在真實的面試中是否遇到過這個題?
yes說明在面試中我是否需要實現kmp演算法?
樣例如果 source ="source"
和 target ="target"
,返回-1
。
如果 source ="abcdabcdefg"
和 target ="bcd"
,返回1
。
挑戰
思路還有一種演算法 bm演算法實際中比kmp演算法效率高很多
void getnext(const char *target, int next)else}}
int strstr(const char *source, const char *target)
else j = next[j];
if(j == len_tag)
}return ans;
}
LintCode 13 字串查詢
對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。如果 source source 和 target target 返回 1。如果 source abcdabcdefg 和 targ...
LintCode 13 字串查詢
對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。說明 在面試中我是否需要實現kmp演算法?樣例 如果 source source 和 target target 返回 1。如果 s...
LintCode 13 字串查詢
題目描述 對於乙個給定的 source 字串和乙個 target 字串,你應該在 source 字串中找出 target 字串出現的第乙個位置 從0開始 如果不存在,則返回 1。說明 在面試中我是否需要實現kmp演算法?不需要,當這種問題出現在面試中時,面試官很可能只是想要測試一下你的基礎應用能力。...