解题:子串判断
2023-09-01 18:42:58
发布于:广东
23阅读
0回复
0点赞
空降坐标:CP003090
请看代码1
#include <bits/stdc++.h>
using namespace std;
string a,b;
int main()
{
cin>>a>>b;
if(a.find(b,0)!=string::npos)
{
cout<<b<<" is substring of "<<a;
}
else if(b.find(a,0)!=string::npos)
{
cout<<a<<" is substring of "<<b;
}
else
{
cout<<"No substring";
}
return 0;
}
请看代码2
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int len1,len2;
bool s1_in_s2(string s1,string s2,int len1,int len2)
{
bool flag;
if(len1>len2)
{
return false;
}
else
{
for(int i=0;i<=len2;i++)
{
flag=true;
if(s1[0]==s2[i])
{
for(int j=1;j<=len1;j++)
{
if(s1[j]!=s2[i+j])
{
flag=false;
break;
}
}
if(flag)
{
return true;
}
}
else
{
continue;
}
}
}
return false;
}
bool s2_in_s1(string s1,string s2,int len1,int len2)
{
bool flag;
if(len2>len1)
{
return false;
}
else
{
for(int i=0;i<=len1;i++)
{
flag=true;
if(s2[0]==s1[i])
{
for(int j=1;j<=len2;j++)
{
if(s2[j]!=s1[i+j])
{
flag=false;
break;
}
}
if(flag)
{
return true;
}
}
else
{
continue;
}
}
}
return false;
}
int main()
{
cin>>s1>>s2;
len1=s1.size()-1;
len2=s2.size()-1;
if(s1_in_s2(s1,s2,len1,len2))
{
cout<<s1<<" is substring of "<<s2;
}
else if(s2_in_s1(s1,s2,len1,len2))
{
cout<<s2<<" is substring of "<<s1;
}
else
{
cout<<"No substring";
}
return 0;
}
方法2相当于自己写一个find函数了 有点废 但是也能AC
这里空空如也
有帮助,赞一个