SDKD 2017 Spring Team Training B H题Pair: normal and paranormal or URAL - 2019( 栈应用)


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 n lowercase letters and exactly n uppercase letters.
Output
If the problem has a solution, output n space-separated integers g 1g 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 i must be pairwise different. If the problem has several solutions, output any of them. If the problem has no solution, output “Impossible”.
Example
input output
2
AbBa
2 1
2
AbaB
Impossible
1
Ab
Impossible

题意当真晦涩难懂, 又是幽灵 又是射手 啥的 射击  bb了半天   这一看半圆 还以为是个数学题

大体意思:   输入n  然后输入2*n 个字符 里面有大小写,,  然后问  对应的大小写之间 会不会交叉  例如 A-b-a-B 这样就交叉了   如果是A-b-B-a  A-a 包括b和B  不会交叉 所以可以   然后 要输入大写对应的 小写的 序号   注意(大小写计数 是分开的)

所以 既然是 A-a 的问题 就可以类比到( --)  左括号与有括号的问题   所以用stack 栈 就非常好用   

进栈入栈操作 

注意:!::  我们要记得是 大写对应的 小写的序号   ASCII 中 小写 大于大写 

用一个结构体  存序号和字符;

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <vector>

const int N=121314;
using namespace std;

string s;
struct node{
    int id;
    char x;
};
int n;
int res[N];

void px()
{
    memset(res,0,sizeof(res));
    int i;
    stack<node> Q;
    int cont1=1,cont2=1;
    for(i=0;i<2*n;i++)
    {
        node p;
        p.x=s[i];
        if(p.x>='a'&&p.x<='z')
            p.id=cont2++;
        if(p.x>='A'&&p.x<='Z')
            p.id=cont1++;
        if(Q.size())
        {
            node temp=Q.top();
            Q.pop();
            if(temp.x+32==p.x)
                 res[temp.id]=p.id;
            else if(p.x+32==temp.x)
                res[p.id]=temp.id;
            else
            {
                Q.push(temp);//没有对应匹配的 再回到栈中
                Q.push(p);
            }
        }
        else
            Q.push(p);// 栈空 进栈
    }
    if(Q.size())//如果 栈中 还有没有匹配的  完蛋
        printf("Impossible\n");
    else
    {
         printf("%d",res[1]);
         for(i=2;i<=n;i++)
         {
            printf(" %d",res[i]);
         }
        puts("");
    }
}
int main()
{
    while(cin>>n)
    {
        s.clear();
        cin>>s;
        px();
        memset(res,0,sizeof(res));
    }
    return 0;
}



posted @ 2017-05-02 20:53  Sizaif  阅读(173)  评论(0编辑  收藏  举报