-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_Pattern_KMP_Algorithm.cpp
53 lines (48 loc) · 1.16 KB
/
Search_Pattern_KMP_Algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector<int> search(string pat, string txt)
{
vector<int> ans;
for(int i = 0; i <= txt.size() - pat.size(); i++)
{
bool match = true;
for(int j = 0; j < pat.size(); j++)
{
if(pat[j] != txt[i + j])
{
match = false;
break;
}
}
if(match) ans.push_back(i + 1);
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
string S, pat;
cin >> S >> pat;
Solution ob;
vector <int> res = ob.search(pat, S);
if (res.size()==0)
cout<<-1<<endl;
else {
for (int i : res) cout << i << " ";
cout << endl;
}
}
return 0;
}
// Contributed By: Pranay Bansal
// } Driver Code Ends