You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
502 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
int find(string p, string s) {
int n = s.size(), i = 0;
int m = p.size(), j = 0;
for (i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++)
if (p[j] != s[i + j]) break;
if (j == m) return i;
}
return -1;
}
int main() {
string txt = "aaacaaab", pat = "aaab";
cout << find(pat, txt) << endl;
// 答案4
// 表示从索引号为4开始存在第一个匹配
return 0;
}