Skip to content

Update and optimization for 11_homework_3_answer.cpp #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions 18-Programming-4kids/11_homework_3_answer.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
#include<iostream>
#include <iostream>

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