POJ-1068(模拟题)

本文详细解析了如何通过模拟括号字符串,实现括号匹配算法,以计算右括号匹配区间内的左括号数量。文章提供了完整的代码实现,涵盖了括号配对、区间计算等关键步骤。

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

题意概述:给你n个数p[i],代表第i个右括号之前的左括号个数,让你输出一个w数组。w[i]代表与其相匹配的左括号的区间内的右括号的个数

思想:我们可以先根据p数组模拟出这个字符串,然后在由字符串反推出w数组 

 代码实现

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#include<math.h>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define PI acos(-1)
#define close ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define open  #ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout); #endif
using namespace std;
typedef long long ll;
const double pai=3.141592653589793238462643383279;
const int MAX_N = 1000000+50;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll INF_ll = 0x7fffffffffffffff;
ll mod = 1e9+7;

/*
不要放弃

	Don't give up
	
心中有梦(ICPC)
*/

inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=10*x+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void Out(int a)
{
	if(a>9)
		Out(a/10);
	putchar(a%10+'0');
}

multiset<int> s;

int p[MAX_N],vis[MAX_N],w[MAX_N];

int main()
{
	string s;
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		s="";
		for(int i = 1; i <= n; i++) cin>>p[i];
		p[0] = 0;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= (p[i]-p[i-1]); j++)
				s+="(";
			s+=")";
		}
		memset(vis,0,sizeof(vis));
		int k = 1;
		for(int i = 0; i < 2*n; i++){
			int ans = 1;
			if(s[i] == ')'){
				for(int j = i-1; j >= 0; j--){
					if(s[j] == ')'){
						ans++;
					}
					if(s[j] == '(' && !vis[j]){
						vis[j] = 1;
						break;
					}
				}
				w[k++] = ans;
			}
		}
		for(int i = 1; i <= n; i++){
			cout<<w[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

/*
                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃货智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值