S
S
Sergey Zolotarev2020-09-30 20:15:45
PHP
Sergey Zolotarev, 2020-09-30 20:15:45

What is the correct way to get a negative result of a substring?

Good evening!
Recently, I encountered the fact that after the second test of my solution to a problem in PHP, I did not receive the required result that I should have done it in accordance with the conditions for solving the problem.

The essence of the problem is this and what are the conditions for its solution:
y4mZtJszfaB22i0Z7IqY-HvOSWmuFtzKTNqFeb20QaRyNSBgBa2NQqCthcp3129GR0mmPyJKIKzD3vAks7SZeioDyikyhVDg8RPrbUy_h6AzrDnYnHgLk9McDxuS7JwJWMEliJTPh3P1rS_QRKvdJoIZouva6wTBcRU7wSB-MtK8M24yVEjiFE9O8SHWI3vpcDR9Q0p3T-qQFhmBSLZCW74XQ? width = 1800 & height = 1200 & cropmode = none
y4mKLav5aKHE45Pd600FGt36I-WtQnrLza2EsX_ZM4to7WUJipxtZaMNJCDsfrAJyxzHOb_DLe3w0gJqOcXLJvLMhNhR7LGV5GGRAEoGmy5pOcG2oXo8-8pqfUVv794GwqRiDp04u6nQ_UftZ8txxLvtmRybpz_3SQcPO37eHCpOYrtklfthtGpT4SLuqG6A_KmlzbbBNLt0eelCHZTpdu43w? width = 1800 & height = 1200 & cropmode = none

The first test passed successfully in accordance with the conditions of the solution, and the second test brought bad surprises, giving various compiler messages:

PHP Notice:  String offset cast occurred in <код решения> on line 45
PHP Notice:  String offset cast occurred in <код решения> on line 46

The code of the problem solution (according to which the compiler displayed messages) in the comments to my question, so that you can know what I asked you about and so that you can help me with this ;-)
How to properly check for negative substrings so that -1 is displayed, if the program correctly does not give the correct substrings?
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-09-30
@seregazolotaryow64

In addition to the obvious problems, you have an invalid palindromic check.
Your code, for example, will consider the string "aab" as a palindrome.
Then, for some reason, the binpoisk is added here. Why is not at all clear.
Also, your solution is too slow. Here the complexity is of the order of O(n^2 log n), which for 200000 will not fit into any restrictions.
For the correct solution, only one observation is needed - for any palindrome, you can take the middle of length 2 or 3 - this will be a shorter palindrome. Therefore, if there are at least some palindromes in the string, then there are definitely palindromes of length 2 or 3.

decision

Надо проверить, есть ли 2 подряд идущих одинаковых символа. Если есть - лексикографически минимальная такая пара - ответ.
Иначе, надо проверить, есть ли тройка символов, где первый равен третьему. Из всех таких надо выбрать лексикографически минимальную.
Если и таких нет, то ответ "-1".
Задача решается за 2 не вложенных цикла.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question