1. 程式人生 > >判斷一個字串是否是另一個字串的子串

判斷一個字串是否是另一個字串的子串

#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;
}