Task: | Z-array |
Sender: | arnxxau |
Submission time: | 2024-11-04 01:04:28 +0200 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.15 s | details |
#7 | ACCEPTED | 0.14 s | details |
#8 | ACCEPTED | 0.14 s | details |
#9 | ACCEPTED | 0.13 s | details |
#10 | ACCEPTED | 0.14 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.15 s | details |
#13 | ACCEPTED | 0.14 s | details |
Compiler report
input/code.cpp: In function 'int main()': input/code.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 49 | for (int i = 0; i < z_array.size(); i++) { | ~~^~~~~~~~~~~~~~~~ input/code.cpp:51:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 51 | if (i < z_array.size() - 1) cout << " "; | ~~^~~~~~~~~~~~~~~~~~~~
Code
#include <iostream> #include <vector> #include <string> using namespace std; vector<int> calculateZ(string s) { int n = s.size(); // Tamaño del string vector<int> Z(n, 0); // Inicializar la Z-array con 0s Z[0] = n; // Z[0] es siempre igual al tamaño del string int L = 0, R = 0; // Inicializa los extremos de la ventana de coincidencia más reciente for (int i = 1; i < n; i++) { if (i > R) { // No hay coincidencia anterior que pueda reutilizarse L = R = i; // Comparación directa del substring desde i con el prefijo de s while (R < n && s[R] == s[R - L]) { R++; } Z[i] = R - L; // Longitud del substring que coincide con el prefijo R--; // Ajustar R para que sea el índice del último carácter coincidente } else { int k = i - L; // Posición relativa en el substring que se está reutilizando // Si el valor de Z en la posición k no supera el final de la ventana actual if (Z[k] < R - i + 1) { Z[i] = Z[k]; // Podemos reutilizar el valor de Z[k] } else { // Necesidad de expandir la ventana L = i; while (R < n && s[R] == s[R - L]) { R++; } Z[i] = R - L; R--; } } } return Z; } int main() { string s; cin >> s; // Leer el string de entrada vector<int> z_array = calculateZ(s); // Calcular la Z-array // Imprimir la Z-array for (int i = 0; i < z_array.size(); i++) { cout << z_array[i]; if (i < z_array.size() - 1) cout << " "; } cout << endl; return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
aaaaaaaaaa |
correct output |
---|
10 9 8 7 6 5 4 3 2 1 |
user output |
---|
10 9 8 7 6 5 4 3 2 1 |
Test 2
Verdict: ACCEPTED
input |
---|
ababababab |
correct output |
---|
10 0 8 0 6 0 4 0 2 0 |
user output |
---|
10 0 8 0 6 0 4 0 2 0 |
Test 3
Verdict: ACCEPTED
input |
---|
aabababaaa |
correct output |
---|
10 1 0 1 0 1 0 2 2 1 |
user output |
---|
10 1 0 1 0 1 0 2 2 1 |
Test 4
Verdict: ACCEPTED
input |
---|
ahtqqkhrrn |
correct output |
---|
10 0 0 0 0 0 0 0 0 0 |
user output |
---|
10 0 0 0 0 0 0 0 0 0 |
Test 5
Verdict: ACCEPTED
input |
---|
mqdozmqdoz |
correct output |
---|
10 0 0 0 0 5 0 0 0 0 |
user output |
---|
10 0 0 0 0 5 0 0 0 0 |
Test 6
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 999999 999998 999997 9... |
user output |
---|
1000000 999999 999998 999997 9... Truncated |
Test 7
Verdict: ACCEPTED
input |
---|
ababababababababababababababab... |
correct output |
---|
1000000 0 999998 0 999996 0 99... |
user output |
---|
1000000 0 999998 0 999996 0 99... Truncated |
Test 8
Verdict: ACCEPTED
input |
---|
baaaabbabaabbbbaaaabbbababaaba... |
correct output |
---|
1000000 0 0 0 0 1 2 0 3 0 0 1 ... |
user output |
---|
1000000 0 0 0 0 1 2 0 3 0 0 1 ... Truncated |
Test 9
Verdict: ACCEPTED
input |
---|
ltpbdybvcpychbsplyonjfmdtsnqwt... |
correct output |
---|
1000000 0 0 0 0 0 0 0 0 0 0 0 ... |
user output |
---|
1000000 0 0 0 0 0 0 0 0 0 0 0 ... Truncated |
Test 10
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 99998 99997 99996 9999... |
user output |
---|
1000000 99998 99997 99996 9999... Truncated |
Test 11
Verdict: ACCEPTED
input |
---|
abaababaab |
correct output |
---|
10 0 1 3 0 5 0 1 2 0 |
user output |
---|
10 0 1 3 0 5 0 1 2 0 |
Test 12
Verdict: ACCEPTED
input |
---|
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz... |
correct output |
---|
1000000 499999 499998 499997 4... |
user output |
---|
1000000 499999 499998 499997 4... Truncated |
Test 13
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 147418 147417 147416 1... |
user output |
---|
1000000 147418 147417 147416 1... Truncated |