题解 B2118 【验证子串】
验证一个字符串是否为另一个字符串的子串,我们可以用 string 库里的函数来实现。
string str="hello world";
此时我们有了一个 string 类型的变量
str.find("hello")
这样可以调用
如果该函数的返回值为 str.npos (表示无效),则在
Code
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main()
{
cin>>a>>b;
if(a.find(b)!=a.npos) //如果b是a的子串
{
cout<<b<<" is substring of "<<a<<endl;
}
else if(b.find(a)!=b.npos) //如果a是b的子串
{
cout<<a<<" is substring of "<<b<<endl;
}
else //如果没有子串关系
{
cout<<"No substring"<<endl;
}
return 0;
}