2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest Problem H. Pair: normal and paranormal

时间:2021-01-14 18:40:42

题目链接:http://codeforces.com/group/aUVPeyEnI2/contest/229669

时间限制:1s

空间限制:64MB

题目大意:给定一个长度为2n,由n个大写字母和n小写字母组成的字符串,将对应的字母两两连接,且不相交,按顺序输出没个大写字母对应的小写字母的位置,如果不存在则输出"Impossible"

样例:

2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest   Problem H. Pair: normal and paranormal

2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest   Problem H. Pair: normal and paranormal

解法:

手动建立结构体,维护一个栈

代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
char v;
int id;
}st[11000];
int main()
{
int n;
char s[11000];
int ans[11000]={0};
int sum1=0,sum2=0;
int top=0;
int tot=0;
cin>>n;
for(int i=1;i<=n*2;i++)
{
cin>>s[i];
if(isupper(s[i]))
{
sum1++;
if(s[i]+32==st[top].v)
{
ans[sum1]=st[top].id;
top--;
}
else
{
top++;
st[top].id=sum1;
st[top].v=s[i];
}
}
else
{
sum2++;
if(s[i]-32==st[top].v)
{
ans[st[top].id]=sum2;
top--;
}
else
{
top++;
st[top].v=s[i];
st[top].id=sum2;
}
}
}
if(top!=0)cout<<"Impossible"<<endl;
else
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
return 0;
}