CF--Ultimate Army--模拟栈

本文介绍了一个复杂的军队组织结构解析问题,通过解读一个由数字和括号组成的字符串来确定每个士兵的直接上司,从而构建出整个军队的组织架构。文章提供了一段C++代码,展示了如何读取输入的字符串,解析每个士兵的ID及其下属,并输出每个士兵的上司ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

I. Ultimate Army

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

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 idid of the soldier is written then following it xx 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)2(3(4))(1)"{} means that soldier 22 is the supervisor of soldiers 33 and 11, and soldier 33 is the supervisor of soldier 44, while soldiers 11 and 44 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+SubordinatesSoldier=Id+Subordinates

Subordinates=(Soldier)+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×1051≤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 106106.

It's guaranteed that each idid from 11 to nn appears exactly once in the string.

Output

Output in a single line nn space separated integers, the iith 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

Copy

4
2(3(4))(1)

output

Copy

2 0 2 3

input

Copy

7
4(2)(5(3(6)(1))(7))

output

Copy

3 4 5 0 4 3 5

 

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 1000000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m;
int vis[maxn];
char ch[maxn];
stack<int>s;
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",ch);
    int len=strlen(ch);
    int num=0;
    for(int i=0; i<len; i++)
    {
        if(ch[i]>='0'&&ch[i]<='9')
        {
            num=num*10+ch[i]-'0';
        }
        else
        {
            if(!num)
            {
                if(ch[i]==')')
                {
                    s.pop();//当没有数时,而且此时为右括号,则出栈
                }
            }
            else//有数时
            {
                if(s.size())
                {
                    vis[num]=s.top();//取栈顶的
                }
                if(ch[i]=='(')
                {
                    s.push(num);//若是左括号,则进栈
                }
            }
            num=0;
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(i!=n)
        {
            printf("%d ",vis[i]);
        }else
        {
            printf("%d\n",vis[i]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值