Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
2438 - Pal
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Rezolvare == <syntaxhighlight lang="python" line> def is_palindrome(num): return str(num) == str(num)[::-1] def next_palindrome(num): num += 1 while not is_palindrome(num): num += 1 return num def prev_palindrome(num): num -= 1 while not is_palindrome(num): num -= 1 return num def find_longest_magic_sequence(palindromes): n = len(palindromes) dp = [[0] * n for _ in range(n)] max_length = 1 for i in range(n): dp[i][i] = 1 for length in range(2, n + 1): for i in range(n - length + 1): j = i + length - 1 if length == 2 and palindromes[i] == palindromes[j]: dp[i][j] = 2 max_length = max(max_length, 2) elif palindromes[i] == palindromes[j] and dp[i + 1][j - 1] == length - 2: dp[i][j] = length max_length = max(max_length, length) return max_length def main(): with open("pal.in", "r") as fin: p = int(fin.readline()) n = int(fin.readline()) numbers = list(map(int, fin.readline().split())) if p == 1: correct_numbers = [next_palindrome(num) for num in numbers] with open("pal.out", "w") as fout: fout.write(" ".join(map(str, correct_numbers))) elif p == 2: max_length = find_longest_magic_sequence(numbers) with open("pal.out", "w") as fout: fout.write(f"{max_length}\n") elif p == 3: max_length = find_longest_magic_sequence(numbers) max_palindromes = [] for i in range(n): for j in range(i + 1, n): if numbers[i] == numbers[j]: palindromes = [numbers[k] for k in range(i, j + 1)] if find_longest_magic_sequence(palindromes) == max_length: max_palindromes = palindromes with open("pal.out", "w") as fout: fout.write(" ".join(map(str, max_palindromes))) if __name__ == "__main__": main() </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width