-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathpermpal.cc
49 lines (46 loc) · 965 Bytes
/
permpal.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// https://www.codechef.com/FEB18/problems/PERMPAL
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef unordered_map<char, int> mci;
typedef unordered_map<char, vi> mcvi;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
mcvi p;
mci f;
for (int i = 0; i < n; i++) {
p[s[i]].push_back(i + 1);
f[s[i]]++;
}
int odd = 0;
for (auto x : f) odd += x.second % 2;
if (odd > n % 2) {
cout << "-1\n";
continue;
}
vi a(n);
int i = 0;
for (auto pr : p) {
vi &v = pr.second;
for (int j = 0; j < v.size(); j++) {
if (j == v.size() - 1) a[n / 2] = v[j];
else {
a[i++] = v[j++];
a[a.size() - i] = v[j];
}
}
}
for (i = 0; i < n; i++) {
cout << a[i];
if (i < n - 1) cout << " ";
else cout << endl;
}
}
}