-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching_01.cpp
92 lines (78 loc) · 1.71 KB
/
matching_01.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <string.h> // memset
#include <algorithm> // lower_bound
#include <iomanip>
#include <bitset>
// #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sd second
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define MAXN 120000
const int INF = 0x3f3f3f3f;
vector<int> naive_mathing(const string& s, const string &t) {
const int sn = s.size();
const int tn = t.size();
vector<int> v;
// s can't be in t
if(sn > tn) {
v.push_back(-1);
return v;
}
int index;
for(int i = 0; i < tn; ++i) {
index = i;
for(int j = 0; j < sn && index < tn; ++j) {
if(s[j] == t[index]) {
if(j == sn-1) {
v.push_back(i);
break;
}
++index;
} else {
break;
}
}
}
if(v.empty()) {
v.push_back(-1);
}
return v;
}
// t < s
// O(mn)
vector<int> naive_matching_kmp(const string& s, const string &t) {
const int n = s.size();
const int m = t.size();
vector<int> v;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(i + j >= n || t[j] != s[i + j]) {
v.push_back(0);
break;
} else {
v.push_back(j);
}
}
}
return v;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
cin >> s >> t;
vector<int> v = naive_mathing(s, t);
for(auto i : v) {
cout << i << ' ';
}
cout << endl;
return 0;
}