Problem Description
3 days, that's how much time the king gave Daffy to find him the ultimate army organization plan before he cuts his head off.
2 days already passed with no progress, but luckily Bugs came to the rescue, he gave Daffy the "ultimate"{} plan as a string, unfortunately Daffy couldn't understand this string, now only 4 hours remain.
A soldier can be described in Bugs's string as this: first the id of the soldier is written then following it x brackets () each for a subordinate of this soldier, each subordinate is described inside their bracket in the same way, for example the following string "2(3(4))(1)"{} means that soldier 2 is the supervisor of soldiers 3 and 1, and soldier 3 is the supervisor of soldier 4, while soldiers 1 and 4 doesn't supervise any soldiers. The string Bugs gave you describes the king(he has no supervisor) and his subordinates and their subordinates and so on.
Or more formally:
- Soldier=Id+Subordinates
- Subordinates=(Soldier)+Subordinates|ϕ
where ϕ is the empty string.
Can you figure out the supervisor of each soldier and save Daffy's head?
Input
In the first line you're given an integer nn(1≤n≤1.4×105), the number of soldiers(including the king) in the army.
In the second line you're given Bugs's string as described above, the string's length is less than or equal to 106.
It's guaranteed that each idid from 1 to n appears exactly once in the string.
Output
Output in a single line n space separated integers, the ith of these integers is the supervisor of the iith soldier or 00 if this soldier has no supervisor(he's the king, notice that there will be only one such soldier).
Examples
Input
4
2(3(4))(1)Output
2 0 2 3
Input
7
4(2)(5(3(6)(1))(7))Output
3 4 5 0 4 3 5
题意:有 n 个人,给出一个字符串,数字代表人的编号,数字后的括号代表其下属,要求输出每个人的直接上级
思路:使用栈,一个栈存储括号,一个栈存储编号,开始时对整个字符串最外围加上一个括号,然后进行模拟即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int num[N];
int vis[N];//0表示左括号,-1表示右括号
stack<int> S1;
stack<int> S2;
int main() {
int n;
scanf("%d",&n);
string str;
cin>>str;
str="("+str+")";
int cnt=0;
int tot=0;
for(int i=0; i<str.size(); ++i) {
if(str[i]=='(')
vis[++cnt]=0;
else if(str[i]==')')
vis[++cnt]=-1;
else {
while(true) {
if(str[i]=='(' || str[i]==')') {
i--;
vis[++cnt]=tot;
tot=0;
break;
}
tot=tot*10+str[i]-'0';
i++;
}
}
}
S1.push(0);//数字
S2.push(0);//括号
for(int i=2; i<=cnt; ++i) {
if(vis[i]!=0 && vis[i]!=-1) {
num[vis[i]]=S1.top();
S1.push(vis[i]);
} else if(vis[i]==0)
S2.push(0);
else if(vis[i]==-1) {
S1.pop();
S2.pop();
}
}
for(int i=1; i<n; ++i)
printf("%d ",num[i]);
printf("%d\n",num[n]);
return 0;
}