3 11 1001110110 101 110010010010001 1010 110100010101011
3 0 3
题解:简单kmp,strstr函数也可以;
strstr代码:
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<queue> #include<cstdlib> using namespace std; const int INF=0x3f3f3f3f; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) typedef long long LL; const int MAXN=1010; char A[15],B[MAXN]; int main(){ int N; SI(N); while(N--){ scanf("%s%s",A,B); int ans=0,t=-1; while(true){ if(strstr(B+t+1,A))t=strstr(B+t+1,A)-B; else break; ans++; } printf("%d\n",ans); } return 0; }
kmp
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<queue> #include<cstdlib> using namespace std; const int INF=0x3f3f3f3f; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) typedef long long LL; const int MAXN=1010; char A[15],B[MAXN]; int p[MAXN]; void getp(){ int i=0,j=-1; p[0]=-1; int len=strlen(A); while(i<len){ if(j==-1||A[j]==A[i]){ i++;j++; p[i]=j; } else j=p[j]; } } int kmp(){ getp(); int i=0,j=0; int len=strlen(B); int ans=0; while(i<len){ if(j==-1||B[i]==A[j]){ i++;j++; if(j==strlen(A))ans++; } else j=p[j]; } return ans; } int main(){ int T; SI(T); while(T--){ scanf("%s%s",A,B); printf("%d\n",kmp()); } return 0; }
Binary String Matching(kmp+str)
原文:http://www.cnblogs.com/handsomecui/p/5233496.html