URAL - 2019
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,cnt=1;
string str;
stack<pair<char,int> >stk;
map<int,int>mmp;
map<int,int>kill;
cin>>n>>str;
for(int i=0; i<2*n; i++)
{
if(str[i]>='a'&&str[i]<='z')
mmp[i]=cnt++;
}
for(int i=0; i<2*n; i++)
{
if(!stk.empty())
{
if(fabs(str[i]-stk.top().first)==32)
{
if((str[i]>='a'&&str[i]<='z')&&(stk.top().first>='A'&&stk.top().first<='Z'))
{
kill[stk.top().second]=i;
stk.pop();
}
else if((str[i]>='A'&&str[i]<='Z')&&(stk.top().first>='a'&&stk.top().first<='z'))
{
kill[i]=stk.top().second;
stk.pop();
}
else
stk.push(make_pair(str[i],i));
}
else
stk.push(make_pair(str[i],i));
}
else
stk.push(make_pair(str[i],i));
}
cnt=0;
if(!stk.empty())
cout<<"Impossible"<<endl;
else
{
for(int i=0; i<2*n; i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
if(cnt>0)
cout<<" "<<mmp[kill[i]];
else
cout<<mmp[kill[i]];
cnt++;
}
}
cout<<endl;
}
return 0;
}