标签:uva
题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=36239
题意:给你序列的区间和的正负,要求构造一组序列满足条件。
转换为前缀和,进行拓扑序列。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
char s[123];
int n;
int sum[110];
int du[110], is[15][15];
void toposort()
{
queue<int> que;
while (!que.empty()) que.pop();
for (int i = 0; i <= n; i++)
if (du[i] == 0) que.push(i);
int tmp = 0;
while (!que.empty())
{
int t = que.front(); que.pop();
tmp++;
for (int i = 0; i <= n; i++)
{
if (is[t][i])
{
du[i]--;
if (du[i] == 0)
{
que.push(i);
sum[i] = tmp;
}
}
}
}
return;
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
scanf(" %s", s);
memset(sum, 0, sizeof(sum));
memset(du, 0, sizeof(du));
memset(is, 0, sizeof(is));
int pos = 0;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
{
if (s[pos] == ‘+‘)
{
is[i - 1][j] = 1;
du[j]++;
}
else if (s[pos] == ‘-‘)
{
is[j][i - 1] = 1;
du[i - 1]++;
}
pos++;
}
toposort();
for (int i = 1; i <= n; i++)
printf("%d%c", sum[i] - sum[i - 1], i == n ? ‘\n‘ : ‘ ‘);
}
return 0;
}
版权声明:转载请注明出处。
标签:uva
原文地址:http://blog.csdn.net/u014427196/article/details/47359527