-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10132.cpp
More file actions
141 lines (131 loc) · 3.14 KB
/
10132.cpp
File metadata and controls
141 lines (131 loc) · 3.14 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
#include <iostream>
#include <set>
#include <cstring>
using namespace std;
char* strings[300][2];
set<int> lengths;
const int DEBUG=0;
bool test(char* str,int lens){
bool result=true;
int limit=lens/2;
//cout<<str<<endl;
for(set<int>::reverse_iterator it=lengths.rbegin();it!=lengths.rend();++it){
int len=*it;
if(len<=limit) break;
for(int i=0;i<1;++i){
//cout<<strings[len][i]<<endl;
if(strings[len][i][0]==0) continue;
char temp=str[len];
str[len]=0;
if(strcmp(str,strings[len][i])==0){
//cout<<"A"<<endl;
str[len]=temp;
bool ok=false;
for(int j=0;j<2;++j){
//cout<<strings[lens-len][j]<<endl;
if(strcmp(str+len,strings[lens-len][j])==0) {/*cout<<"B"<<endl;*/ok=true;break;}
}
if(ok) continue;
}
str[len]=temp;
if(strcmp(str+(lens-len),strings[len][i])==0){
temp=str[lens-len];
str[lens-len]=0;
bool ok=false;
for(int j=0;j<2;++j){
if(strcmp(str,strings[lens-len][j])==0) {ok=true;break;}
}
str[lens-len]=temp;
if(ok) continue;
}
result=false;
break;
}
}
return result;
}
int main(){
for(int i=1;i<300;++i){
strings[i][0]=new char[i+1];
strings[i][1]=new char[i+1];
}
int kase;
cin>>kase;
char buf[300];
cin.getline(buf,300);
cin.getline(buf,300);
for(int i=0;i<kase;++i){
for(set<int>::iterator it=lengths.begin();it!=lengths.end();++it){
strings[*it][0][0]=strings[*it][1][0]=0;
}
lengths.clear();
int total_length=0;
int total_frag=0;
while(1){
cin.getline(buf,300);
if(*buf==0) break;
if(cin.eof()) break;
int len=strlen(buf);
if(DEBUG) cout<<len<<" "<<buf<<endl;
if(strings[len][0][0]==0)
strcpy(strings[len][0],buf);
else if (strcmp(strings[len][0],buf)!=0)
strcpy(strings[len][1],buf);
lengths.insert(len);
++total_frag;
total_length+=len;
}
int len=total_length/(total_frag/2);
//int half_len=len/2;
int maxlen=*(lengths.rbegin());
int minlen=len-maxlen;
if(DEBUG) cout<<len<<" "<<maxlen<<" "<<minlen<<endl;
if(strings[maxlen][1][0]==0){
if(DEBUG) cout<<"CASE 1";
if(strings[minlen][1][0]!=0){
if(DEBUG) cout<<"A"<<endl;
cout<<strings[maxlen][0]<<strings[minlen][0]<<endl;
goto break1;
}
strcpy(buf,strings[maxlen][0]);
strcat(buf,strings[minlen][0]);
if(test(buf,len)){
if (DEBUG) cout<<"B"<<endl;
cout<<buf<<endl;
goto break1;
}
if (DEBUG) cout<<"C"<<endl;
cout<<strings[minlen][0]<<strings[maxlen][0]<<endl;
goto break1;
}else{
if (DEBUG) cout<<"CASE 2";
if(strings[minlen][1][0]==0){
if(DEBUG) cout<<"A"<<endl;
cout<<strings[maxlen][0]<<strings[minlen][0]<<endl;
goto break1;
}
char temp1[300];
char temp2[300];
strcpy(temp1,strings[maxlen][0]);
temp1[minlen]=0;
if(strcmp(temp1,strings[minlen][0])==0)
strcpy(temp2,strings[minlen][1]);
else
strcpy(temp2,strings[minlen][0]);
temp1[minlen]=strings[maxlen][0][minlen];
strcpy(buf,temp1);
strcat(buf,temp2);
if(test(buf,len)){
if(DEBUG) cout<<"B"<<endl;
cout<<buf<<endl;
goto break1;
}
if(DEBUG) cout<<"C"<<endl;
cout<<temp2<<temp1<<endl;
goto break1;
}
break1:
if(i!=kase-1) cout<<endl;
}
return 0;
}