hdoj 1361 Parencodings (模拟)

思路:

由p写出s,s里的括号进行配对,如果两个括号是配对的,那么括号里的括号应该都是配对的,既左括号数=右括号数

代码如下:

#include <iostream>  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int find(char *temp,int xb)
{
	
	int i;
	int x;
	int zuo, you;
	int r;
	
	r = 0;
	for (i = xb-1; i >= 0; i--)
	{
		if (temp[i] == '(')
		{
			zuo = 0;
			you = 0;
			for (x = i + 1; x < xb; x++)
			{
				if (temp[x] == '(')
					zuo++;
				else
					you++;

			}
			if (zuo == you)
			{
				r = i;
				break;
			}
		}
	}
	return r;
}
int main()
{
	int t;
	int n;
	int i;
	int p[50];
	int w[50];
	int xb[50];
	char s[50];
	int max;
	int x;
	int z;
	int r;
	cin >> t;
	while (t--)
	{
		memset(s, 0, sizeof(s));
		cin >> n;
		for (i = 0; i < n; i++)
		{
			cin >> p[i];
			s[p[i] + i] = ')';
			
		}
		n *= 2;
		x = 0;
		for (i = 0; i <n; i++)
		{
			if (s[i] != ')')
			{
				s[i] = '(';
				
			}
			else
				xb[x++] = i;
				
		}
		
		for (i = 0; i < x; i++)
		{
			r = 1;
			z = find(s, xb[i]);
			for (; z < xb[i]; z++)
			{
				if (s[z] == ')')
					r++;
			}
			if (i)
				printf(" %d", r);
			else
				printf("%d", r);
		}

		printf("\n");
		

	}
	
	
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值