KMP
出现次数
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char a[10005],b[1000000];
void getnext(char *t,int *next)
{
int i=0,j=-1,len=strlen(t);
next[0]=-1;
while(i<len)
{
if(j==-1 || t[i]==t[j])
{
i++,j++;
next[i]=j;
}
else
j=next[j];
}
}
int ans;
void kmp(char *s,char *t)
{
int i=0,j=0,len1=strlen(s),len2=strlen(t),next[10005];
getnext(t,next);
while(i<len1 && j<len2)
{
if(j==-1 || s[i]==t[j])
i++,j++;
else
j=next[j];
if(j==len2)//统计子串出现次数
{
j=next[j];
ans++;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>a>>b;
ans=0;
kmp(b,a);
cout<<ans<<endl;
}
return 0;
}
出现位置
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<string>
using namespace std;
void getnext(string t,int len,int *next)
{
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1 || t[i]==t[j])
{
i++,j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp(string s,string t)
{
int i=0,j=0,next[10005];
int len1=s.size(),len2=t.size();
getnext(t,len2,next);
while(i<len1 && j<len2)
{
if(j==-1 || s[i]==t[j])
i++,j++;
else
j=next[j];
}
if(j==len2)
return i-len2+1;
else
return -1;
}
string a,b;
int main()
{
while(getline(cin,b))
{
getline(cin,a);
cout<<kmp(a,b)<<endl;
}
return 0;
}
Horspool
#include <iostream>
#include <string>
using namespace std;
int Horspool(string s,string t,int pos)
{
int len1=s.length(),len2=t.length();
int ti=len2-1; //目标字符串匹配位置
int si=pos+ti; //源字符串匹配位置
if(len1-pos<len2)
return -1;
while(ti>=0 && si<len1)
{
if(s[si]==t[ti])
ti--,si--;
else
{
while(ti>=0 && s[si]!=t[ti])
ti--;
si+=len2-1-ti;
ti=len2-1;
}
}
if(si<len1)
return si+1;
else
return -1;
}
int main()
{
string src,tar;
while(cin>>tar>>src)
{
cout<<Horspool(src,tar,0)<<endl;
}
return 0;
}
BM
#include <iostream>
#include <string>
#include <cstring>
const int maxn=102005;
using namespace std;
void build_bad(string pattern,int bad[])
{
int len=pattern.length();
memset(bad,len,sizeof(bad));
for(int i=0;i<len;i++)
bad[pattern[i]-'A']=len-1-i;
}
void build_good(string pattern,int good[])
{
int len=pattern.length(),k=0;
memset(good,len,sizeof(good));
good[len-1]=1;
char p[maxn];
strcpy(p,pattern.c_str());//将string转化为char{]
for(int i=len-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(memcmp(p+i,p+j,(len-i)*sizeof(char))==0)//比较内存区域buf1和buf2的前count个字节。
{
if(j==0)
k=len-i;
else if(p[i-1]!=p[j-1])
good[i-1]=j-1;
}
}
}
for(int i=0;i<len-1;i++)
{
if(good[i]!=len)
good[i]=len-1-good[i];
else
{
good[i]=len-1-i+good[i];
if(k && k<=len-1-i)
good[i]-=k;
}
}
}
int Boyer_Moore(string pattern,string text)
{
int bad[maxn],good[maxn],len1=pattern.length(),len2=text.length();
build_bad(pattern,bad);
build_good(pattern,good);
int i=len1-1,j=len1-1;
while(j<len2)
{
while(i && pattern[i]==text[j])
i--,j--;
if(i==0 && pattern[i]==text[j])
return j;
else
j+=max(good[i],bad[text[j]-'A']);
i=len1-1;
}
return -1;
}
int main()
{
string a,b;
while(cin>>a>>b)
{
cout<<Boyer_Moore(a,b)<<endl;
}
return 0;
}
Sunday
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int Sunday(string s,string t)
{
int len1=s.length(),len2=t.length(),pos=0,next[26],i,j;
memset(next,len2,sizeof(next));//init
for(i=0;i<len2;i++)//set
next[t[i]-'A']=len2-i;//移动的越大
while(pos<len1-len2+1)
{
i=pos;
for(j=0;j<len2;j++,i++)
{
if(s[i]!=t[j])
{
pos+=next[s[len2+pos]-'A'];//一旦不匹配,原串就按照next跳转
break;
}
}
if(j==len2)
return pos;
}
return -1;
}
int main()
{
string src,tar;
while(cin>>tar>>src)
{
cout<<Sunday(src,tar)<<endl;
}
return 0;
}