A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it’s new tablet Lastus 3000.
This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol’s artificial intelligence.
Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol’s director decided to replace some characters in AI name with “#”. As this operation is pretty expensive, you should find the minimum number of characters to replace with “#”, such that the name of AI doesn’t contain the name of the phone as a substring.
Substring is a continuous subsequence of a string.
Input
The first line of the input contains the name of AI designed by Gogol, its length doesn’t exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn’t exceed 30. Both string are non-empty and consist of only small English letters.
Output
Print the minimum number of characters that must be replaced with “#” in order to obtain that the name of the phone doesn’t occur in the name of AI as a substring.
Examples
input
intellect
tell
output
1
input
google
apple
output
0
input
sirisiri
sir
output
2
思路:
用KMP的方法进行字符串比较(暴力也可)
每次搜索完一个单词后将指针归零
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int N=100005;
int F[N];
char a[N],b[N];
int sum;
void getF (char *P, int m)
{
F[1]=0;
for(int i=2;i<=m;i++)
{
int k=F[i-1];
while(k&&P[k+1]!=P[i]) k=F[k];
if(P[k+1]==P[i]) k++;
F[i]=k;
}
}
void findMatch (char *P, int m,char *T, int n)
{
sum=0;
getF(T,n);
int k=0;
for(int i=1;i<=m;i++)
{
while(P[i]!=T[k+1]&&k) k=F[k];
if(P[i]==T[k+1]) k++;
if(n==k)
{
sum++;
k=0;
}
}
cout<<sum<<endl;
}
int main()
{
scanf("%s %s",a+1,b+1);
findMatch(a,strlen(a+1),b,strlen(b+1));
return 0;
}