判断一个字符串是否是另一个字符串的子串

已知两个字符串A,B,判断B是否是A 的子串

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>

using namespace std;

bool judge(const string &a,const string &b)
{
    int i,j;
    if(a.length()<b.length())return false;
    for(i=0;i<a.length();i++){
        for(j=0;j<b.length();j++){
            if(a[i+j]!=b[j])
                break;   // 字符不相等,退出
        }
        if(j==b.length()) // 达到了b.length,说明字符全部相等
            return true;
    }
    return false;
}
int main()
{
    string A,B;
    while(cin >>A>>B){
        if(judge(A,B))cout <<"YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

   

猜你喜欢

转载自blog.csdn.net/hnlg311709000526/article/details/81330635