怎么优化,不知道哪里存在问题
查看原帖
怎么优化,不知道哪里存在问题
1136226
yhm_22344楼主2024/3/26 15:05
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

int n, m, tem, res = 0;
string s;
bool st[N];
int cnt[N];

bool flag, op;
void dfs(int u)
{
	if (flag)	return;
	if (u > n)
	{
		res++;
		if (res == m + 1)
		{
			for (int i = 1; i <= n; i++)	printf("%d ", cnt[i]);
			flag = true;
			return;
		}
		return;
	}

	for (int i = 1; i <= n; i++)
	{
		if (!res)	i = s[u - 1] - '0';
		if (!st[i])
		{
			st[i] = true;
			cnt[u] = i;
			dfs(u + 1);
			st[i] = false;
		}
	}
}


int main()
{
	scanf_s("%d%d", &n, &m);
	for (int i = 1, x = 0; i <= n; i++)	scanf_s("%d", &x), s += (x + '0');
	dfs(1);
	return 0;
}
2024/3/26 15:05
加载中...