poj1068

#include<iostream>
#include<cstring>
int num[25],result[25];
char s[150];
int main()
{
	using namespace std;
	int n,m;
	cin >> n;
	while(n--){
		cin >> m;
		for(int i=0;i<m;++i)
			cin >> num[i];
		int ans=0;
		int t=0;
		for(int i=0;i<m;++i){
			if(ans!=num[i]){
				int c=num[i]-ans;for(int j=t;j<t+c;++j)	s[j]=‘(‘;
				t+=c;s[t]=‘)‘;++t;ans=num[i];
			}
			else{
				s[t]=‘)‘;++t;
			}
		}
		int tot=0;ans=0;
		for(int i=0;i<t;++i){
			if(s[i]==‘)‘){
				++tot;
				int you=1;
				for(int j=i-1;j>=0;j--){
					if(s[j]==‘)‘)	++you,++tot;
					else if(s[j]==‘(‘)	--you;
					if(!you)	break;
				}
				result[ans++]=tot;
				tot=0;
			}
		}
		for(int i=0;i<ans-1;++i)
			cout << result[i] << " ";
			cout << result[ans-1] << endl;
	}
}

  

时间: 2025-01-01 14:34:25

poj1068的相关文章

poj1068题解报告与感想

poj1068描述了这样一个问题:给出一种括号序列的表示形式名叫P序列,规则是统计出每个右括号之前的左括号个数作为序列每项的值.然后要求你根据这个求括号列的W序列值,W序列的规则是统计每一个右括号和与其匹配的左括号之间所有匹配后的括号个数. 刚看到这个题的时候,想法是十分简单的,就是模拟.我先用给的P序列推出括号序列,然后再去求W序列.后来我在discuss区发现一种十分不错的做法,贴在这里. 假定输入的P序列存在数组p中,则p[i]表示第i个P序列元素. 根据P序列的定义,每个数值表示的是当前

poj1068 模拟

Parencodings Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two differen

poj1068解题报告(模拟类)

POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S         (((()()()))) P-sequence      4 5 6666   (Pi表示第i个右括号前面有多少个左括号) W-sequence     1 1 1456    (Wi表示第i个右括号对应它前面的第几个左括号) 要求给出P串,求W. 思路: 1. 模拟类题型.将输入的P串先装换为S串,再由S串得到W串. 2. 左

[POJ1068]Parencodings

试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence). q By an in

快速切题 poj1068

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19716   Accepted: 11910 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

NUC_HomeWork1 -- POJ1068

A - Parencodings Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p

POJ-1068题

下面的代码是北京大学Online Judge网站上1068题(网址:http://poj.org/problem?id=1068)的所写的代码. 该题的难点在于实现括号匹配,我在代码中采取用-1和1分别代表左右括号,使得括号匹配时各位数相加为0,不匹配时则不为0的方法来判断是否完成括号匹配,代码列表如下: 性能:Memory:704K,Time:0MS #include "iostream" using namespace std; int main() { const int LEF

POJ1068——Parencodings

Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-s

poj1068 Parencodings【模拟】【刷题计划】

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27375   Accepted: 16094 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn