hdu4985水题,做个排列的划分

本文介绍了一种方法,在C++程序中通过循环查找并打印给定序列中的循环子序列,使用了基本的数据结构和算法,如数组、循环、条件判断等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 1000000000
#define maxn 100005
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define mset(x) memset(x,0,sizeof(x))
typedef __int64 ll;

int n;
int a[maxn];
bool h[maxn];
vector<int> v;

int main(){
//	freopen("a.txt","r",stdin);
//	freopen(".out","w",stdout);
	while(cin>>n){
		rep(i,1,n)
			cin>>a[i];
		mset(h);

		rep(i,1,n){
			if(h[i]) continue;
			v.clear();
			int s=i;
			while(!h[s]){
				v.push_back(s);
				h[s]=1;
				s=a[s];
			}

			printf("(%d",v[0]);
			for(int i=1; i<v.size(); i++){
				printf(" %d",v[i]);
			}
			printf(")");	
		}
		puts("");
	}
	return 0;
}

/*
DESCRIPTION:

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值