-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathABCD.cpp
More file actions
66 lines (59 loc) · 1.76 KB
/
ABCD.cpp
File metadata and controls
66 lines (59 loc) · 1.76 KB
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
#include<stdio.h>
char str1[1000021],str2[1000021];
int a[4]={0},temp[4],index[4]={0,1,2,3},pos=0,n,t_index[4];
void sort(){
for(int i=0;i<4;i++){
for(int j=0;j<3-i;j++){
if(temp[j]>temp[j+1]){
int t=temp[j];
temp[j]=temp[j+1];
temp[j+1]=t;
t=t_index[j];
t_index[j]=t_index[j+1];
t_index[j+1]=t;
}
}
}
}
int isValid(char c,int p){
if(p==0){if(str1[p]==c)return 0;else return 1;}
else if(str1[p]==c||str2[p-1]==c)return 0;
else return 1;
}
int flag=0;
void best_fit(){
if(flag==1)return;
if(pos==2*n)flag=1;
for(int i=0;i<4;i++){
temp[i]=a[i];
t_index[i]=index[i];
}
sort();
for(int i=0;i<4;i++){
//for(int k=0;k<4;k++)printf("temp:%d\tindex:%d\t",temp[k],t_index[k]);
//printf("\n");
//printf("%c\t",t_index[i]+65);
if((isValid(t_index[i]+65,pos)==0)||a[t_index[i]]==n)continue;
//printf("%c\t%s\t",t_index[i]+65,str2);
//printf("\n");
str2[pos]=char(t_index[i]+65);
pos++;
if(pos==2*n){flag=1;printf("%s",str2);return;}
int t1=t_index[i];
a[t1]++;
best_fit();
a[t1]--;
pos--;
}
}
int main(){
scanf("%d",&n);
scanf("%s",&str1);
for(int i=0;i<(n<<1);i++){
a[(str1[i]-65)]++;
}
best_fit();
//for(int i=0;i<2*n;i++)
//printf("%s",str2);
return 0;
}