Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
46 views2 pages

#Include Bitsstdc++.h

The document contains C++ code to generate strings from an array of integers representing character positions. It initializes variables, reads test cases with input arrays, generates strings if possible or outputs -1, and checks for valid character positions throughout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

#Include Bitsstdc++.h

The document contains C++ code to generate strings from an array of integers representing character positions. It initializes variables, reads test cases with input arrays, generates strings if possible or outputs -1, and checks for valid character positions throughout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
using namespace std;
int T, n, a[20010];
string s;

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("ZSTRING.INP", "r", stdin);
//freopen("ZSTRING.OUT", "w", stdout);
cin >> T;
for (int iTest = 1; iTest <= T; iTest++) {
cin >> n;
bool oce = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] >= 27)
oce = 0;
}
if (a[1] == 1 && n == 1) {
cout << "z\n";
continue;
}
if (a[1] == 1 || oce == 0) {
cout << -1 << '\n';
continue;
}
s = "z";
a[1]--;
int pos = 24;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
pos = 1;
int d = 1;
while (d < a[i]) {
if (pos < 0 || pos > 25) {
oce = 0;
break;
}
s += ('a' + pos);
pos++;
d++;
}
if (i < n)
pos = max(pos, a[i+1]);
if (pos < 0 || pos > 25) {
oce = 0;
break;
}
s += ('a' + pos);
}
else {
pos = a[i] - 1;
int d = 1;
while (d <= a[i]) {
if (pos < 0 || pos > 25) {
oce = 0;
break;
}
s += ('a' + pos);
pos--;
d++;
}
}
if (oce == 0) break;
}
if (oce == 0) cout << -1 << '\n';
else cout << s << '\n';
}
}

You might also like