首页 技术 正文
技术 2022年11月11日
0 收藏 610 点赞 4,023 浏览 1265 个字

算法指南白书

思路:“连续和转化成前缀和之差”

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn=;
int T,n;
int fa[];
char str[][],s[];
int G[][];
int c[];
vector<int>topo; int findset(int x){//并查集求祖先
return fa[x]!=x?fa[x]=findset(fa[x]):x;
}
bool dfs(int u){//拓扑排序
c[u]=-;
for(int i=;i<=n;i++){
if(G[u][i]){
if(c[i]<)
return false;
if(c[i]==)
dfs(i);
}
}
c[u]=;
topo.push_back(u);
return true;
}
bool toposort(){
topo.clear();
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
if(!c[i]){
if(!dfs(i))
return false;
}
}
reverse(topo.begin(),topo.end());
return true;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
getchar();
scanf("%s",s);
int index=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
str[i][j]=s[index++];
if(str[i][j]=='')
fa[j]=i-;//i-1和j同祖先
}
}
memset(G,,sizeof(G));
for(int i=;i<=n;i++){//sum[i]<sum[j],i到j连一条边
for(int j=i;j<=n;j++){
if(str[i][j]=='-')
G[findset(j)][findset(i-)]=;
if(str[i][j]=='+')
G[findset(i-)][findset(j)]=;
}
}
toposort();
int sum[];
memset(sum,,sizeof(sum));
int cur=;
for(int i=;i<=n;i++){//0到。。。赋值
sum[topo[i]]=cur++;
}
for(int i=;i<=n;i++){
sum[i]=sum[findset(i)];
if(i>)
printf(" ");
printf("%d",sum[i]-sum[i-]);
}
printf("\n");
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289