Pagini recente » Cod sursa (job #2596037) | Cod sursa (job #2338459) | Cod sursa (job #553060) | Cod sursa (job #2132281) | Cod sursa (job #2514470)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("ahocorasick.in");ofstream fout("ahocorasick.out");
void computelps(char*pat,int m,int* lps){
int i=1;int len=0;lps[0]=0;
while(i<m){
if(pat[i]==pat[len]){
len++;lps[i]=len;i++;
}
else if(len!=0){
len=lps[len-1];
}
else{
lps[i]=0;i++;
}
}
}
int searchs(char* txt,char* pat){
int n=strlen(txt);int m=strlen(pat);
int lps[m];computelps(pat,m,lps);int i=0,j=0;int cnt=0;
while(i<n){
if(pat[j]==txt[i]){
i++;j++;
}
if(j==m){cnt++;j=lps[j-1];}
else{
if(i<n && pat[j]!=txt[i]){
if(j!=0){
j=lps[j-1];
}
else{
i++;
}
}
}
}
return cnt;
}
int main(){
int nl;char txt[1000000];fin>>txt;fin>>nl;char pat[10000];
for(int a=0;a<nl;a++){
fin>>pat;fout<<searchs(txt,pat)<<endl;;
}
return 0;
}