Joa*_*son 32
你可以做点什么
int count = 0;
const char *tmp = myString;
while(tmp = strstr(tmp, string2find))
{
count++;
tmp++;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,当您得到结果时,再次开始在字符串的下一个位置搜索.
strstr()不仅从字符串的开头起作用,而且从任何位置起作用.
应该已经处理过的部分字符串是否应该被消费?
例如,有什么期待答案搜索的情况下oo在foooo,2或3?
如果是后者(我们允许子串重叠,答案是三个),那么Joachim Isaksson 建议使用正确的代码.
如果我们搜索不同的子串(答案应该是两个),那么请参阅下面的代码(以及此处的在线示例):
char *str = "This is a simple string";
char *what = "is";
int what_len = strlen(what);
int count = 0;
char *where = str;
if (what_len)
while ((where = strstr(where, what))) {
where += what_len;
count++;
}
Run Code Online (Sandbox Code Playgroud)小智 5
使用KMP,您可以在 O(n) 中完成
int fail[LEN+1];
char s[LEN];
void getfail()
{
//f[i+1]= max({j|s[i-j+1,i]=s[0,j-1],j!=i+1})
//the correctness can be proved by induction
for(int i=0,j=fail[0]=-1;s[i];i++)
{
while(j>=0&&s[j]!=s[i]) j=fail[j];
fail[i+1]=++j;
if (s[i+1]==s[fail[i+1]]) fail[i+1]=fail[fail[i+1]];//optimizing fail[]
}
}
int kmp(char *t)// String s is pattern and String t is text!
{
int cnt=0;
for(int i=0,j=0;t.s[i];i++)
{
while(j>=0&&t.s[i]!=s[j]) j=fail[j];
if (!s[++j])
{
j=fail[j];
cnt++;
}
}
return cnt;// how many times s appeared in t.
}
Run Code Online (Sandbox Code Playgroud)