我該怎麼做了就地相當於strstr()
的用C計串(即不空值終止)?的strstr()的字符串,它是不是空終止
回答
如果你害怕O(m * n個)的行爲 - 基本上,你不用,這樣的情況不會自然發生 - 這裏有一個KMP實現我已經躺在附近我已經修改採取乾草堆的長度。也是一個包裝。如果您想重複搜索,請自行編寫並重新使用borders
陣列。
沒有缺陷保證,但它似乎仍然有效。
int *kmp_borders(char *needle, size_t nlen){
if (!needle) return NULL;
int i, j, *borders = malloc((nlen+1)*sizeof(*borders));
if (!borders) return NULL;
i = 0;
j = -1;
borders[i] = j;
while((size_t)i < nlen){
while(j >= 0 && needle[i] != needle[j]){
j = borders[j];
}
++i;
++j;
borders[i] = j;
}
return borders;
}
char *kmp_search(char *haystack, size_t haylen, char *needle, size_t nlen, int *borders){
size_t max_index = haylen-nlen, i = 0, j = 0;
while(i <= max_index){
while(j < nlen && *haystack && needle[j] == *haystack){
++j;
++haystack;
}
if (j == nlen){
return haystack-nlen;
}
if (!(*haystack)){
return NULL;
}
if (j == 0){
++haystack;
++i;
} else {
do{
i += j - (size_t)borders[j];
j = borders[j];
}while(j > 0 && needle[j] != *haystack);
}
}
return NULL;
}
char *sstrnstr(char *haystack, char *needle, size_t haylen){
if (!haystack || !needle){
return NULL;
}
size_t nlen = strlen(needle);
if (haylen < nlen){
return NULL;
}
int *borders = kmp_borders(needle, nlen);
if (!borders){
return NULL;
}
char *match = kmp_search(haystack, haylen, needle, nlen, borders);
free(borders);
return match;
}
:哦,哇,我一定會嘗試這個!謝謝! :) – Mehrdad 2011-12-21 05:37:44
看看下面的功能是否適合你。我沒有徹底測試過,所以我建議你這樣做。
char *sstrstr(char *haystack, char *needle, size_t length)
{
size_t needle_length = strlen(needle);
size_t i;
for (i = 0; i < length; i++)
{
if (i + needle_length > length)
{
return NULL;
}
if (strncmp(&haystack[i], needle, needle_length) == 0)
{
return &haystack[i];
}
}
return NULL;
}
這實際上與我目前使用的類似,但它是O(mn),而(我假設)'strstr'是O(m + n)。所以我正在尋找一些不像我的版本那麼慢的東西。 :-)但無論如何,因爲這個想法很有效。 – Mehrdad 2011-12-21 03:24:36
@Mehrdad:也許值得一窺這個實現:http://src.gnu-darwin.org/src/lib/libc/string/strnstr.c.html – 2011-12-21 03:26:09
哇,我想我錯了那麼......所以'strstr'通常被定義爲一個O(mn)操作?感謝您指出這一點...然後我可能會接受這一點,因爲它是問題的確切替代品。 – Mehrdad 2011-12-21 03:27:40
我剛剛遇到這個,我想分享我的實施。它認爲它相當快,我沒有任何subcalls。
它返回找到指針的乾草堆中的索引,如果找不到則返回-1。
/* binary search in memory */
int memsearch(const char *hay, int haysize, const char *needle, int needlesize) {
int haypos, needlepos;
haysize -= needlesize;
for (haypos = 0; haypos <= haysize; haypos++) {
for (needlepos = 0; needlepos < needlesize; needlepos++) {
if (hay[haypos + needlepos] != needle[needlepos]) {
// Next character in haystack.
break;
}
}
if (needlepos == needlesize) {
return haypos;
}
}
return -1;
}
當你在它的時候,應該繼續做Boyer-Moore;) – 2016-10-27 20:02:15
- 1. fgets()總是空終止它返回的字符串嗎?
- 2. SQL字符串空終止?
- 3. 空終止字符串
- 4. C字符串空終止
- 5. 是否printf終止每個字符串與空字符?
- 6. wcscat_s字符串不爲空終止
- 7. 什麼是空終止ASCII字符串的正則表達式?
- 8. C字符串的空終止
- 9. JavaScript WebSocket非空終止的字符串
- 10. 雙空終止的字符串
- 11. 儘管字符串有空終止字符,std :: string的長度是否一致?
- 12. 如何知道字符串是否爲空終止
- 13. 空終止字符串:使用'\ 0'還是隻有0?
- 14. Re2是否使用字符串大小或空終止?
- 15. C:Eclipse/gdb和空終止字符串
- 16. 終止字符串
- 17. 什麼是轉換字符數組(無空終止)到字符串
- 18. 什麼是正確的字符串終止符在c
- 19. C:字符串Concatenation:空終止字符串
- 20. 使用0而不是'\ 0'終止一個c字符串是錯誤的嗎?
- 21. 我收到錯誤信息:表達式:(L「字符串不是空終止」&&0)
- 22. 爲什麼不在字符串中任意放置空終止符會終止它?
- 23. 什麼是最簡單的方法來防止空字符終止我的字符串?
- 24. 返回的字符串是從getline()終止的嗎?
- 25. UTF-8字符串的簡單加密,其結果是NULL終止字符串?
- 26. 字符串中奇怪的空格字符,那不是空格?
- 27. HTMLInputHidden空終止字符
- 28. 終止unicode空字符
- 29. 空字符串還是不空字符串?
- 30. ComboBox.Text =空字符串,而不是實際顯示的字符串
您必須編寫自己的版本。 – 2011-12-21 03:13:08
哪個字符串不是空終止的?正在搜索的字符串或子字符串? – 2011-12-21 03:15:28
@TimCooper:正在搜索的人(乾草堆)。 – Mehrdad 2011-12-21 03:16:22