赞
踩
- #include <stdio.h>
-
- #include <string.h>
- #define N 100
- using namespace std;
- void Next(int next[],int lenT,char* T)
- {
- int i=0,j=-1;
-
- next[0]=-1;
- while (i<lenT)
- {
- if(j==-1 || T[i]==T[j])
- {
- i++;j++;
- next[i]=j;
- }
- else
- j=next[j];
- }
-
- }
- int KMP(char*S,char*T,int lenS,int lenT)
- {
- int next[lenT];
- Next(next,lenT,T);
- int i=0,j=0;
- while (i<lenS&&j<lenT)
- {
- if(j==-1 || S[i]==T[j])
- {
- i++;j++;
- }
- else
- {
- j=next[j];
- }
- }
- if(j==lenT)
- {
- return i-lenT+1;
- }
- else return 0;
- }
- int main()
- {
- char S[N],T[N];
- int lenS,lenT;
- for(int i =1;i<=3;i++)
- {
- S[N]={0};
- T[N]={0};
- scanf("%s",S);
-
- lenS=strlen(S); //printf("%d\n",lenS );
- scanf("%s",T);
- lenT=strlen(T); //printf("%d", lenT);
-
- printf("%d\n",KMP(S,T,lenS,lenT));
- }
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。