-
Notifications
You must be signed in to change notification settings - Fork 3
Фролов Кирилл Владимирович #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #include <vector> | ||
|
|
||
| void hide_secret(char* const text, const char* const secret) { | ||
| if (text == nullptr || secret == nullptr){ | ||
| return; | ||
| } | ||
|
|
||
| int m = 0; | ||
| const char* temp = secret; | ||
| while (*temp != '\0') { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для определения длины строки можно воспользоваться
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот этот цикл можно было организовать проще (если не заменять на int m = 0;
for (; secret[m] != '\0'; ++m) {}Исчезла необходимость в переменной |
||
| m++; | ||
| temp++; | ||
| } | ||
|
|
||
| if (m == 0) { | ||
| return; | ||
| } | ||
|
|
||
| std::vector<int> lps(m); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут тоже лучше другое имя придумать. Понятно, что можно разобраться в назначении объекта, прочитав код дальше, но гораздо лучше, когда все сразу понятно в момент объявления объекта.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я понял, почему вы такие имена выбрали) У вас алгоритм Кнута-Морриса-Пратта. |
||
|
|
||
| int len = 0; | ||
| lps[0] = 0; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поторопились вектор использовать, мы его еще не проходили) Если ваш код запустить, то получим обращение к неинициализированной памяти. Оператор
Но вообще задание решается спокойно и без динамической памяти.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Проглядел передачу размера в вектор. Нет обращения к неициализированной памяти, ошибся |
||
|
|
||
| int i = 1; | ||
| while (i < m) { | ||
| if (secret[i] == secret[len]) { | ||
| len++; | ||
| lps[i] = len; | ||
| i++; | ||
| } else { | ||
| if (len != 0) { | ||
| len = lps[len - 1]; | ||
| } else { | ||
| lps[i] = 0; | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int j = 0; | ||
| i = 0; | ||
|
|
||
| while (text[i] != '\0') { | ||
| if (secret[j] == text[i]) { | ||
| j++; | ||
| i++; | ||
| } | ||
|
|
||
| if (j == m) { | ||
| for (int k = i - j; k < i; k++) { | ||
| text[k] = 'x'; | ||
| } | ||
| j = lps[j - 1]; | ||
| } else if (text[i] != '\0' && secret[j] != text[i]) { | ||
| if (j != 0) { | ||
| j = lps[j - 1]; | ||
| } else { | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Переменным лучше давать более осмысленные имена. В продакшн коде однобуквенные названия — моветон (у математиков и научных сотрудников только так принято)