学过字符串的初学者专用题解
2026-03-15 17:20:34
发布于:安徽
11阅读
0回复
0点赞
A398.子串判断
题目大意
输入两个字符串,验证其中一个串是否为另一个串的子串。
输入格式
输入两个字符串, 每个字符串占一行,长度不超过 500500 且不含空格。
输出格式
若第一个串 是第二个串 的子串,则输出() is substring of ();
否则,若第二个串 是第一个串 的子串,则输出() is substring of ();
否则,输出 No substring。
思路
只要用 find 就可以解题。
示例代码
#include<bits/stdc++.h>//万能头文件。
using namespace std;
int main(){
string s1,s2;
cin>>s1>>s2;
if(s1.find(s2)/*如果s1里没有s2,则返回“string::npos”*/!=string::npos){
cout<<s2<<" is substring of "<<s1<<endl;
}else if(s2.find(s1)!=string::npos){
cout<<s1<<" is substring of "<<s2<<endl;
}else{
cout <<"No substring"<<endl;
}
return 0;
}
这里空空如也






有帮助,赞一个