1 条题解

  • 0
    @ 2025-8-24 21:07:26

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar 我和鱼过不去
    **

    搬运于2025-08-24 21:07:25,当前版本为作者最后更新于2021-07-04 23:29:19,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    验证一个字符串是否为另一个字符串的子串,我们可以用 string 库里的函数来实现。


    string str="hello world";
    

    此时我们有了一个 string 类型的变量 strstr

    str.find("hello")
    

    这样可以调用 findfind 函数来在 strstr 中寻找是否含有子串 "hello"\mathtt{"hello"}
    如果该函数的返回值为 str.npos (表示无效),则在 strstr 中不存在该子串,否则存在。


    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;
    }
    
    • 1

    信息

    ID
    6975
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    1
    已通过
    1
    上传者