转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=1068
Description
S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456
Input
Output
Sample Input
2 6 4 5 6 6 6 6 9 4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 6 1 1 2 4 5 1 1 3 9
题意:
一组括号 (((( ) ( ) ( ) ) ) )
有两种描述方法:
P方法:4 5 6 6 6 6 - 每一个‘)’前,有几个‘(’
W方法:1 1 1 4 5 6 - 每一个‘)‘的前面第几个(注意是从当前位置往前面数)’(‘是跟它匹配的
要求是根据P求W
思路:
先根据P还原出括号的位置,在计算出W即可;
代码如下:
#include <iostream>
using namespace std;
#include <cstring>
#define N 117
char s[10000];
int n;
int i, j, k, l;
int p[N],w[N], flag[N];
void WW()
{
int x = 1;
int count = 0;
for(i = 1; i < l; i++)
{
if(s[i] == ')')
{
for(j = i-1; j >= 1; j--)
{
if(s[j] == '(')
count++;
if(flag[j] == 0 && s[j] == '(')
{
flag[j] = 1;
w[x++] = count;
break;
}
}
count = 0;
}
}
}
int main()
{
int t;
while(cin >> t)
{
while(t--)
{
memset(flag,0,sizeof(flag));
cin >> n;
k = 0, l = 1;
for(i = 1; i <= n; i++)
{
cin >> p[i];
for(j = 1; j <= p[i] - k; j++)
{
s[l++] = '(';
}
s[l++] = ')';
k = p[i];
}
// cout<<s<<endl;
WW();
for(i = 1; i < n; i++)
{
cout<<w[i]<<' ';
}
cout<<w[n]<<endl;
}
}
return 0;
}
poj 1068 Parencodings(模拟),布布扣,bubuko.com
原文:http://blog.csdn.net/u012860063/article/details/37740201