poj 1068

题目大意:

给出p密码串,p的定义为,找到p字串的每一个')',然后往右数统计'('的个数

求w字串,w的定义为,找到')',然后统计与之匹配的'(',统计这个

分析:

根据w直接模拟就可以了,用了队列和vector容器来实现这个过程。

其实估计足够大的数组就可以了,但还是试着用一下vector。

#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;
ll arr[25], ans[25], number, wal, point;

int main(void)
{
	int t;
	cin >> t;

	while (t--)
	{
		cin>>number;
		for (ll i = 1; i <= number; i++) scanf("%lld", &arr[i]);
		
		queue <char> q;
		ll point = 0;
		for (ll i = 1; i <= number; i++)
		{
			if (point < arr[i])
			{
				for (ll j = point; j < arr[i]; j++)
				{
					q.push('(');
				}
				point = arr[i];
				q.push(')');
			}
			else if (point == arr[i])
			{
				q.push(')');
			}
		}
		vector<char> c;
		vector<int> vis;
		wal = 1;
		while (q.size())
		{
			char temp = q.front();
			q.pop();
			c.push_back(temp);
			vis.push_back(0);
		}
		for (ll i = 0; i <= c.size(); i++)
		{
			//printf("i:%lld ", i);
			if (c[i] == ')')
			{
				ll cnt = 1;
				for (ll j = i-1; j >= 0; j--)
				{
					//printf("j:%lld\n", j);
					if (c[j] == ')') cnt++;
					else if (c[j] == '('&&vis[j]==0)
					{
						//cout << cnt << endl;
						ans[wal] = cnt;
						vis[j] = 1;
						wal++;
						break;
					}
				}
			}
		}
		for (ll i = 1; i < wal; i++)
		{
			cout << ans[i];
			i == wal-1 ? cout << endl : cout << ' ';
		}
	}
}

你可能感兴趣的:(poj 1068)