-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_pattern.cpp
More file actions
58 lines (56 loc) · 1.16 KB
/
search_pattern.cpp
File metadata and controls
58 lines (56 loc) · 1.16 KB
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
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector <int> search(string pat, string txt)
{
//code here.
int ws=pat.length();
int n=txt.length();
vector<int> ans;
int i=0;
int j=0;
while(i<n){
if(txt[i]==pat[j]){
int last=i;
while(txt[i]==pat[j] && j<pat.length() && i<txt.length()){
i++;
j++;
}
if(j==pat.length()){
ans.push_back(last+1);
j=0;
i=last+1;
}
else{
j=0;
i=last+1;
}
}
else{
i++;
}
}
return ans;
}
};
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;
}