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
3374 - Caroiaj
(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="python3" line="1"> def parcurgere_caroiaj(n, caroiaj): rezultat = "" for i in range((n + 1) // 2): # Parcurgere pe chenarul exterior al caroiajului for j in range(i, n - i): rezultat += caroiaj[i][j] # Parcurgere pe chenarul din dreapta for j in range(i + 1, n - i): rezultat += caroiaj[j][n - i - 1] # Parcurgere pe chenarul din jos for j in range(n - i - 2, i - 1, -1): rezultat += caroiaj[n - i - 1][j] # Parcurgere pe chenarul din stanga for j in range(n - i - 2, i, -1): rezultat += caroiaj[j][i] return rezultat def cea_mai_lunga_secventa_simetrica(șir): max_len = 0 start = 0 for i in range(1, len(șir)): # Căutăm palindrom la fiecare caracter j = 0 while i - j >= 0 and i + j < len(șir) and șir[i - j] == șir[i + j]: j += 1 current_len = 2 * j - 1 if current_len > max_len: max_len = current_len start = i - j + 1 return șir[start:start + max_len] def verifica_rezultat(input_file, output_file): with open(input_file, "r") as f: n = int(f.readline()) caroiaj = [list(f.readline().strip()) for _ in range(n)] with open(output_file, "r") as g: rezultat_asteptat = g.readline().strip() sir_construit = parcurgere_caroiaj(n, caroiaj) rezultat_program = cea_mai_lunga_secventa_simetrica(sir_construit) if rezultat_program == rezultat_asteptat: print("Rezultat corect!") else: print("Rezultat incorect!") print("Rezultat așteptat:") print(rezultat_asteptat) print("Rezultat obținut:") print(rezultat_program) # Citire date de intrare with open("caroiajin.txt", "r") as f: n = int(f.readline()) caroiaj = [list(f.readline().strip()) for _ in range(n)] # Construire șir parcurgând caroiajul sir_construit = parcurgere_caroiaj(n, caroiaj) # Determinare cea mai lungă secvență simetrică rezultat = cea_mai_lunga_secventa_simetrica(sir_construit) # Scriere rezultat în fișierul de ieșire with open("caroiajout.txt", "w") as g: g.write(rezultat) # Verificare rezultat pentru exemplul dat verifica_rezultat("caroiajin.txt", "caroiajout.txt") </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