Repository navigation
Expand file tree
/
Copy pathABCD_slow.cpp
More file actions
195 lines (180 loc) · 3.2 KB
/
Copy pathABCD_slow.cpp
File metadata and controls
195 lines (180 loc) · 3.2 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<string>
#include<cstring>
using namespace std;
inline int min(int a,int b)
{
if( a< b ) return a;
else return b;
}
inline int max(int a,int b)
{
if( a>b ) return a;
else return b;
}
enum mask{ N, A,B,C,D };
mask code[50005];
string str;
string out;
int n; // stores the number of each character
int a_c,b_c,c_c,d_c; // will store the count of each number
bool process(int i) // process the i'th position in the seq. -> true means all the current value of mask is valid
{
if( i == 0 ) // if the first position -> no worry about exceeding n
{
if( str[i]=='A' )
{
b_c++;
out[i]='B';
if( process(i+1) ) return true;
b_c--;
c_c++;
out[i]='C';
if( process(i+1) ) return true;
c_c--;
d_c++;
out[i]='D';
if( process(i+1) ) return true;
d_c--;
}
else if( str[i]=='B' )
{
a_c++;
out[i]='A';
if( process(i+1) ) return true;
a_c--;
c_c++;
out[i]='C';
if( process(i+1) ) return true;
c_c--;
d_c++;
out[i]='D';
if( process(i+1) ) return true;
d_c--;
}
else if( str[i]=='C' )
{
a_c++;
out[i]='A';
if( process(i+1) ) return true;
a_c--;
b_c++;
out[i]='B';
if( process(i+1) ) return true;
b_c--;
d_c++;
out[i]='D';
if( process(i+1) ) return true;
d_c--;
}
else if( str[i]=='D' )
{
a_c++;
out[i]='A';
if( process(i+1) ) return true;
a_c--;
b_c++;
out[i]='B';
if( process(i+1) ) return true;
b_c--;
c_c++;
out[i]='C';
if( process(i+1) ) return true;
c_c--;
}
}
else if( i== (str.size()-1) )
{
if( a_c < n && str[i]!='A' && out[i-1]!='A' ) // it should be a
{
a_c++;
out[i]='A';
return true;
}
else if( b_c < n && str[i]!='B' && out[i-1]!='B' )
{
b_c++;
out[i]='B';
return true;
}
else if( c_c < n && str[i]!='C' && out[i-1]!='C' )
{
c_c++;
out[i]='C';
return true;
}
else if( d_c< n && str[i]!='D' && out[i-1]!='D' )
{
d_c++;
out[i]='D';
return true;
}
else
return false; // no.solution exist
}
else
{
if( a_c < n && str[i]!='A' && out[i-1]!='A' ) // it should be a
{
a_c++;
out[i]='A';
if( process(i+1) ) return true;
a_c--;
}
if( b_c < n && str[i]!='B' && out[i-1]!='B' )
{
b_c++;
out[i]='B';
if( process(i+1) ) return true;
b_c--;
}
if( c_c < n && str[i]!='C' && out[i-1]!='C' )
{
c_c++;
out[i]='C';
if( process(i+1) ) return true;
c_c--;
}
if( d_c< n && str[i]!='D' && out[i-1]!='D' )
{
d_c++;
out[i]='D';
if( process(i+1) ) return true;
d_c--;
}
return false; // no.solution exist
}
}
int main()
{
cin>>n;
out.clear(); // the output string
cin>>str; // stores the input seq.
out = string(n*2,'0');
register int i,j;
a_c=0,b_c=0,c_c=0,d_c=0; // the count of various numbers
for(i=0;i<str.size() ;i++)
{
if( str[i]=='A')
a_c++;
else if( str[i] == 'B')
b_c++;
else if( str[i] == 'C' )
c_c++;
else
d_c++;
}
memset(code, 0, sizeof(code) ) ; // set all to none
if( process(0) )
cout<<out<<endl;
else
cout<<"No seq found"<<endl;
return 0;
}