1 条题解
-
0
#include<bits/stdc++.h> using namespace std; int main() { string s, t; getline(cin, s); // [1] 读取父字符串(支持包含空格) getline(cin, t); // [2] 读取子字符串 int n = s.size(); int m = t.size(); bool has_match = false; // [3] 标记是否找到匹配的子串 // [4] 遍历所有可能的起始位置 for (int i = 0; i <= n - m; i++) { bool match = true; // [5] 检查当前起始位置的子串是否与t完全匹配 for (int j = 0; j < m; j++) { if (s[i + j] != t[j]) { match = false; break; } } if (match) { cout << i + 1 << endl; // [6] 输出1-based的起始位置 has_match = true; } } // [7] 若没有找到任何匹配,输出-1 if (!has_match) { cout << -1 << endl; } return 0; }
- 1
信息
- ID
- 125
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 4
- 已通过
- 3
- 上传者