-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPERMPAL.cpp
68 lines (67 loc) · 1.7 KB
/
PERMPAL.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
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
int main()
{
long int t;
cin>>t;
while(t--)
{
string s;
vector<long int>v[26];
cin>>s;
long int cnt[26]={0},si[26]={0},ei[26]={0};
for(long int i=0;s[i]!='\0';i++)
{
cnt[s[i]-'a']++;
v[s[i]-'a'].push_back(i+1);
}
vector<long int> ans(100005);
long int cntodd=0,oddpos=-1;
for(int i=0;i<26;i++)
{
if(cnt[i]%2!=0)
{
cntodd++;
oddpos=i;
}
}
if(cntodd>1)
cout<<"-1"<<endl;
else
{
//si[0]=0;
long int len=s.length();
long int k=0;
for(int i=0;i<26;i++)
{
if(cnt[i]>1&&cnt[i]%2==0)
{
for(long int j=0;j<cnt[i];j+=2)
{
ans[k]=v[i][j];
ans[len-k-1]=v[i][j+1];
k++;
}
}
}
if(cntodd)
{long int pp=cnt[oddpos];
for(long int i=0;i<pp;i+=2)
{
ans[k]=v[oddpos][i];
cnt[oddpos]--;
// cout<<v[oddpos][i]<<" "<<cnt[oddpos]<<endl;
if(cnt[oddpos]>0)
{
ans[len-k-1]=v[oddpos][i+1];
cnt[oddpos]--;
}
k++;
}
}
for(long int i=0;i<len;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
}
}