快使用 find() 函数,嗬嗬哈嘿
2026-08-24 17:33:50
发布于:陕西
0阅读
0回复
0点赞
#include <iostream>
#include <string>
using namespace std;
int main() {
// 优化IO操作
ios::sync_with_stdio(false);
cin.tie(NULL);
string s1, s2;
// 读取两个字符串
if (cin >> s1 >> s2) {
// find函数返回子串首次出现的位置,如果未找到则返回 string::npos
size_t pos1 = s2.find(s1);
size_t pos2 = s1.find(s2);
if (pos1 != string::npos) {
// s1 是 s2 的子串
cout << s1 << " is substring of " << s2 << endl;
} else if (pos2 != string::npos) {
// s2 是 s1 的子串
cout << s2 << " is substring of " << s1 << endl;
} else {
// 互不为子串
cout << "No substring" << endl;
}
}
return 0;
}
这里空空如也







有帮助,赞一个