-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnti virus.c
107 lines (99 loc) · 2.21 KB
/
Anti virus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct tableau_2D
{
int K, deg;
} tab_2D;
void deg(int K, int *Recipient, int *Degre, int Nb)
{
if (Recipient[K - 1] == 0)
Degre[K - 1] = 2;
else
{
if (Degre[Recipient[K - 1] - 1] == -1)
deg(Recipient[K - 1], Recipient, Degre, Nb);
Degre[K - 1] = Degre[Recipient[K - 1] - 1] + 1;
}
}
int s_to_i(char *s)
{
int n = strlen(s), x = 0;
for (int i = 0; i < n; i++)
x += (s[i] - 48) * pow(10, n - i - 1);
return x;
}
void Parcourir(int pos, char *s, int n, int *Recipient, int *Degre, tab_2D *Perdu, int Nb)
{
if (pos < n && s[pos] == '?')
{
int i = (pos) ? 0 : 1;
while (i < 10)
{
s[pos] = 48 + i++;
Parcourir(pos + 1, s, n, Recipient, Degre, Perdu, Nb);
}
s[pos] = '?';
}
if (pos < n && s[pos] != '?')
Parcourir(pos + 1, s, n, Recipient, Degre, Perdu, Nb);
if (pos >= n)
{
int K = s_to_i(s);
Perdu[K - 1].K = K;
if (K > Nb)
return;
deg(K, Recipient, Degre, Nb);
Perdu[K - 1].deg = Degre[K - 1];
}
}
void Permuter(int *A, int *B)
{
int C = *A;
*A = *B;
*B = C;
}
void Tri(tab_2D *Perdu, int n)
{
int i = 0;
while (i < n && Perdu[i].deg == -1)
i++;
i++;
while (i < n)
{
int j = i;
while (j && Perdu[j].deg < Perdu[j - 1].deg)
{
Permuter(&Perdu[j].deg, &Perdu[j - 1].deg);
Permuter(&Perdu[j].K, &Perdu[j - 1].K);
j--;
}
i++;
}
}
int main()
{
int N, *Recipient, *Degre;
tab_2D *Perdu;
scanf("%d", &N);
Recipient = (int *)malloc(N * sizeof(int));
Degre = (int *)malloc(N * sizeof(int));
Perdu = (tab_2D *)malloc(N * sizeof(tab_2D));
for (int i = 0; i < N; i++)
{
scanf("%d ", &Recipient[i]);
Degre[i] = -1;
Perdu[i].deg = -1;
}
char s[6];
scanf("%s", s);
Parcourir(0, s, strlen(s), Recipient, Degre, Perdu, N);
Tri(Perdu, N);
int i = 0;
while (Perdu[i].deg == -1)
i++;
while (i < N)
printf("%d ", Perdu[i++].K);
return 0;
}