diff --git a/18-Programming-4kids/11_homework_3_answer.cpp b/18-Programming-4kids/11_homework_3_answer.cpp index 5e1e78c..29b7034 100644 --- a/18-Programming-4kids/11_homework_3_answer.cpp +++ b/18-Programming-4kids/11_homework_3_answer.cpp @@ -1,29 +1,28 @@ -#include +#include + using namespace std; -int main() { - string big_str, small_str; - cin >> big_str >> small_str; +int main() +{ + string big_str, small_str; + cin>>big_str>>small_str; + bool flag = 0; + int big_size = (int)big_str.size(); + int small_size = (int)small_str.size(); - if (small_str.size() > big_str.size()) { + if (small_size > big_size) { cout << "NO\n"; return 0; } - // For every possible position in big_str, try to match with the small - for (int i = 0; i < (int) big_str.size() - (int) small_str.size() + 1; ++i) { - bool is_match = true; + for(int i = 0, j = 0; i < big_size; i++){ + if(big_str[i] != small_str[j++]){ + j = 0; + } - for (int j = 0; j < (int) small_str.size() && is_match; ++j) { - if (small_str[j] != big_str[i + j]) - is_match = false; - } - if (is_match) { - cout << "YES\n"; - return 0; - } - } - cout << "NO\n"; + if(j == small_size)flag = 1; + } - return 0; + if(flag)cout << "YES\n"; + else cout << "NO\n"; }