forked from volzkzg/sgu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
142.cpp
101 lines (93 loc) · 1.69 KB
/
142.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 1050000;
string s,ans_str;
char str[300];
int L,ans_num;
struct node{
node *left,*right;
// node *parent;
char c;
bool cek;
}buf[maxn],*tree,*ans;
int top = 0,n;
inline void init(node *T,int dep,char cc){
T->c = cc;
T->cek = false;
if (dep<L){
T->left = &buf[top++];
T->right = &buf[top++];
init(T->left,dep+1,'a');
init(T->right,dep+1,'b');
}else{
T->left = NULL;
T->right = NULL;
}
}
inline void find(node *T,int dep){
if (T->cek == false && dep<ans_num){
ans = T;
ans_num = dep;
ans_str= "";
for (int i=1;i<=ans_num;++i)
ans_str+=str[i];
}
if (T->left!=NULL) {
str[dep+1] = 'a';
find(T->left,dep+1);
}
if (T->right!=NULL){
str[dep+1] = 'b';
find(T->right,dep+1);
}
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
cin >> n;
cin >> s;
L = ceil(log(n)/log(2));
node *tree = new node();
tree->c = ' ';
init(tree,0,' ');
for (int i=0;i<s.size()-L+1;++i)
{
node *k = tree;
for (int j=0;j<L;++j)
switch(s[j+i])
{
case 'a':
k = k->left;
k->cek = true;
break;
case 'b':
k = k->right;
k->cek = true;
break;
}
}
for (int i=s.size()-L+1;i<s.size();++i){
node * k = tree;
for (int j=i;j<s.size();++j)
switch (s[j])
{
case 'a':
k = k->left;
k->cek = true;
break;
case 'b':
k = k->right;
k->cek = true;
break;
}
}
tree->cek = true;
ans_num = 0x7FFFFFFF;
find(tree,0);
cout << ans_num << endl;
cout << ans_str << endl;
return 0;
}