Skip to content

Commit b6562e0

Browse files
authored
Create 1753-StringMatching.cpp
using KMP algorithm:- https://cp-algorithms.com/string/prefix-function.html
1 parent 8273608 commit b6562e0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain , give me another chacne to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
#define ll long long
13+
14+
#define mod 1000000007
15+
16+
17+
int main(){
18+
string s,t;
19+
cin>>s>>t;
20+
ll val=t.length();
21+
t+="#";
22+
t+=s;
23+
ll n=t.length();
24+
vector<ll> dp(n,0);
25+
dp[0]=0;
26+
ll ans=0;
27+
for(ll i=1;i<n;i++){
28+
29+
ll temp=dp[i-1];
30+
while(temp>0 && t[temp]!=t[i]){
31+
temp=dp[temp-1];
32+
}
33+
34+
dp[i]=temp;
35+
if(t[i]==t[temp]){
36+
dp[i]++;
37+
}
38+
39+
40+
if(dp[i]==val){
41+
ans++;
42+
}
43+
}
44+
cout<<ans<<endl;
45+
46+
47+
48+
49+
}

0 commit comments

Comments
 (0)