H - Pair: normal and paranormal (栈模拟)

If you find yourself in Nevada at an abandoned nuclear range during Halloween time, you’ll become a witness of an unusual show. Here Ghostbusters hold annual tests for new versions of their proton packs. There are n Ghostbusters and n portable traps with ghosts, all are located on a semicircle. Each trap contains exactly one ghost. The ghosts may be of different types, but each Ghostbuster can neutralize with his weapon only one type of the evil spirits.

On the count of three all ghost traps open at once and all Ghostbusters start to fire. Of course, each Ghostbuster shoots at the ghost, which his proton gun is able to neutralize. The most important thing here is not to cross proton beams of the guns.

Problem illustration

You know positions of all Ghostbusters and all the traps in this year’s tests. For each Ghostbuster determine which ghost he should shoot at, so that all the ghosts are neutralized and no two gun beams cross. You can assume that all proton beams are in the same horizontal plane and they don’t shoot ghosts through in case of a hit.

Input

In the first line there is an integer n that is the number of Ghostbusters (1 ≤ n ≤ 5 000). In the second line the sequence of 2 n Latin letters is written, describing the allocation of the Ghostbusters and the traps on the semicircle. Uppercase letters correspond to the Ghostbusters and lowercase letters correspond to the traps. For example, “a” stands for a trap with the ghost of type “a”, while “A” stands for the Ghostbuster with the gun neutralizing ghosts of type “a”. The sequence has exactly nlowercase letters and exactly n uppercase letters.

Output

If the problem has a solution, output n space-separated integers g 1, g 2, …, g n, where g i is the number of the ghost i-th Ghostbuster should shoot at. Both Ghostbusters and ghosts are numbered with integers from 1 to n in the order of their positions along the semicircle. All g imust be pairwise different. If the problem has several solutions, output any of them. If the problem has no solution, output “Impossible”.

Example

inputoutput
2
AbBa
2 1
2
AbaB
Impossible
1
Ab
Impossible

题目大意:给一个半圆,在圆上有怪兽和猎人居住,猎人用枪打怪兽,但是抢不能交叉,否则无效,大写字母打对应的小谢字母,如果可以的话,输出对应顺序的大写字母,打的是第几个小写字母,例如第一个样例,第一个大写字符A打对应的第二个小写字母a。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 5e4 + 50;
struct node
{
    int id;
    char ch;
};
stack<node> st;
string str;
int ans[maxn];
int main()
{
    memset(ans, 0, sizeof(ans));
    int n;
    cin >> n;
    cin >> str;
    int da = 0, xiao = 0;
    for(int i=0; i<str.size(); i++)//遍历整个串
    {
        int pos;
        bool flag = false;
        if(str[i] >= 'A' && str[i] <= 'Z') pos = ++da, flag = true;//计算大写个数,并标记该字符为大写
        else pos = ++xiao;//计算小写个数
        if(st.empty()) st.push((node) {pos, str[i]});//栈为空压栈
        else
        {
            node now = st.top();
            if(abs(now.ch - str[i]) == 32)//为对应字母
            {
                st.pop();
                if(flag) ans[pos] = now.id;//大写 ans[大写位置] = 小写位置
                else ans[now.id] = pos;//同理
            }
            else st.push((node) {pos, str[i]});//不对应继续压栈
        }
    }
    if(st.empty())
    {
        for(int i=1; i<=da; i++)
        {
            cout << ans[i] << ' ';
        }
        cout << endl;
    }
    else cout << "Impossible" << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值