-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1)permutation_palindrome.cpp
63 lines (53 loc) · 992 Bytes
/
1)permutation_palindrome.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int getChar(int i){
return (char)(i+97);
}
void printPalindromicString(string s){
map< char,vector<int> > Indexes;
for(int i=0;i<s.length();i++){
Indexes[s[i]].push_back(i);
}
int odd_count=0;
int ans[s.length()];
for(int i=0;i<26;i++){
if(Indexes[getChar(i)].size() % 2 ==1){
odd_count++;
}
}
if(odd_count>1){
cout<<"-1";
return;
}
else{
int start =0;
int end = s.length()-1;
for(int i=0;i<Indexes.size();i++){
char cur_Char = getChar(i);
for(int j=0;j<Indexes[cur_Char].size();j+=2){
if((Indexes[cur_Char].size() - j )== 1){
ans[s.length()/2]= Indexes[cur_Char][j];
continue;
}
ans[start] = Indexes[cur_Char][j];
ans[end] = Indexes[cur_Char][j+1];
start++;
end--;
}
}
}
for(int i=0;i<s.length();i++){
cout<<ans[i]+1<<" ";
}
}
int main()
{
int t;
cin>>t;
while(t--){
string s;
cin>>s;
printPalindromicString(s);
cout<<endl;
}
}