QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#22846 | #1662. ASCII Automata Art | ezoilearner# | AC ✓ | 38ms | 4636kb | C++17 | 3.7kb | 2022-03-10 18:14:56 | 2022-04-30 01:47:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct Canvas{
int w,h;
vector<string> c;
Canvas(int w,int h):w(w),h(h){
c.resize(w);
for(int i=0;i<w;i++)c[i].resize(h,' ');
}
string& operator[](int x){return c[x];}
const string& operator[](int x)const{return c[x];}
Canvas move(int dx,int dy){
Canvas nw(w+dx,h+dy);
for(int i=0;i<w;i++)for(int j=0;j<h;j++)nw[i+dx][j+dy]=c[i][j];
return nw;
}
void operator|=(const Canvas &b){
for(int i=0;i<b.w;i++)for(int j=0;j<b.h;j++)if(c[i][j]==' ')c[i][j]=b[i][j];
}
};
inline bool isA(char a){return 'A'<=a&&a<='Z';}
Canvas expr(string s);
Canvas atom(string s){
if(s.front()=='('&&s.back()==')')return expr(s.substr(1,s.size()-2));
bool flg=true;
for(int i=0;i<s.size();i++)flg&=isA(s[i]);
if(flg){
Canvas r(3,s.size()+4);
r[0]=r[2]="+"+string(s.size()+2,'-')+"+";
r[1][0]=r[1][s.size()+3]='+';
for(int i=0;i<s.size();i++)r[1][i+2]=s[i];
return r;
}else if(s.back()=='+'){
Canvas c=atom(s.substr(0,s.size()-1));
Canvas r(c.w+2,c.h+6);
r|=c.move(0,3);
r[1].replace(0,3,"+->");
r[1].replace(c.h+3,3,"->+");
for(int i=2;i<r.w;i++)r[i][0]=r[i][r.h-1]='|';
r[r.w-1]="+<"+string(c.h+3,'-')+"+";
return r;
}else if(s.back()=='?'){
Canvas c=atom(s.substr(0,s.size()-1));
Canvas r(c.w+3,c.h+6);
r|=c.move(3,3);
r[1]="+"+string(c.h+3,'-')+">+";
for(int i=2;i<4;i++)r[i][0]=r[i][r.h-1]='|';
r[4].replace(0,3,"+->");
r[4].replace(c.h+3,3,"->+");
return r;
}else{
Canvas c=atom(s.substr(0,s.size()-1));
Canvas r(c.w+5,c.h+6);
r|=c.move(3,3);
r[1]="+"+string(c.h+3,'-')+">+";
for(int i=2;i<4;i++)r[i][0]=r[i][r.h-1]='|';
r[4].replace(0,3,"+->");
r[4].replace(c.h+3,3,"->+");
for(int i=5;i<r.w;i++)r[i][0]=r[i][r.h-1]='|';
r[r.w-1]="+<"+string(c.h+3,'-')+"+";
return r;
}
}
Canvas term(string s){
vector<string> stk;
for(int i=0;i<s.size();i++){
if(isA(s[i])){
if(!stk.empty()&&isA(stk.back().back()))stk.back()+=s[i];
else stk.emplace_back(1,s[i]);
}else if(s[i]=='+'||s[i]=='?'||s[i]=='*'){
char c=stk.back().back();
if(isA(c)){
stk.back().pop_back();
if(stk.back().empty())stk.pop_back();
stk.push_back(string(1,c)+s[i]);
}else stk.back()+=s[i];
}else{
stk.push_back("(");
int c=1;
while(c){
char ch=s[++i];
if(ch=='(')c++;
else if(ch==')')c--;
stk.back()+=ch;
}
}
}
vector<Canvas> vec;
int sw=0,sh=-2,sp=0;
for(string &s:stk){
Canvas c=atom(s);
sw=max(sw,c.w);
sh+=c.h+2;
vec.push_back(c);
}
Canvas r(sw,sh);
int d=0;
for(int i=0;i<vec.size();i++){
Canvas &c=vec[i];
r|=c.move(0,d);
if(i+1<vec.size())r[1].replace(c.h+d,2,"->");
d+=c.h+2;
}
return r;
}
Canvas expr(string s){
s+='|';
vector<Canvas> vec;
int sw=-1,sh=0,cc=0;
string lst;
for(int i=0;i<s.size();i++){
if(s[i]=='(')cc++;
if(s[i]==')')cc--;
if(!cc&&s[i]=='|'){
Canvas c=term(lst);
lst="";
sw+=c.w+1;
sh=max(sh,c.h);
vec.push_back(c);
}else lst+=s[i];
}
if(vec.size()==1)return vec[0];
Canvas r(sw,sh+6);
int d=0;
for(int i=0;i<vec.size();i++){
Canvas &c=vec[i];
r|=c.move(d,3);
r[d+1].replace(0,3,"+->");
r[d+1].replace(c.h+3,sh-c.h+1,string(sh-c.h+1,'-'));
r[d+1].replace(sh+4,2,">+");
if(i+1<vec.size())d+=c.w+1;
}
for(int i=2;i<=d;i++)if(r[i][0]==' ')r[i][0]=r[i][r.h-1]='|';
return r;
}
Canvas input(string s){
Canvas c=expr(s);
Canvas r(c.w,c.h+6);
r|=c.move(0,3);
r[1].replace(0,3,"S->");
r[1].replace(c.h+3,3,"->F");
return r;
}
int main(){
string s;
cin>>s;
Canvas ans=input(s);
cout<<ans.w<<" "<<ans.h<<endl;
for(int i=0;i<ans.w;i++){
cout<<ans.c[i];
if(i+1<ans.w)cout<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3568kb
input:
NE?(ER)C++|(IS)*?|(CHA((LL))ENGING)
output:
23 57 +---+ +----+ +---+ S->+->+ N +->+-------->+->+ ER +->+->+->+ C +->+->+->+->F | +---+ | | +----+ | | +---+ | | | | | +---+ | | | | | | | +->+ E +->+ | +<--------+ | | |...
result:
ok 23 57
Test #2:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
N(ER)C
output:
3 26 +---+ +----+ +---+ S->+ N +->+ ER +->+ C +->F +---+ +----+ +---+
result:
ok 3 26
Test #3:
score: 0
Accepted
time: 3ms
memory: 3732kb
input:
C|ON|TEST
output:
11 20 +---+ S->+->+ C +---->+->F | +---+ | | | | +----+ | +->+ ON +--->+ | +----+ | | | | +------+ | +->+ TEST +->+ +------+
result:
ok 11 20
Test #4:
score: 0
Accepted
time: 3ms
memory: 3656kb
input:
A+
output:
5 17 +---+ S->+->+ A +->+->F | +---+ | | | +<--------+
result:
ok 5 17
Test #5:
score: 0
Accepted
time: 3ms
memory: 3668kb
input:
B?
output:
6 17 S->+-------->+->F | | | +---+ | +->+ B +->+ +---+
result:
ok 6 17
Test #6:
score: 0
Accepted
time: 3ms
memory: 3732kb
input:
C*
output:
8 17 S->+-------->+->F | | | +---+ | +->+ C +->+ | +---+ | | | +<--------+
result:
ok 8 17
Test #7:
score: 0
Accepted
time: 3ms
memory: 3652kb
input:
Z
output:
3 11 +---+ S->+ Z +->F +---+
result:
ok 3 11
Test #8:
score: 0
Accepted
time: 1ms
memory: 3664kb
input:
((((DEF)|(GH)|(I))))
output:
11 19 +-----+ S->+->+ DEF +->+->F | +-----+ | | | | +----+ | +->+ GH +-->+ | +----+ | | | | +---+ | +->+ I +--->+ +---+
result:
ok 11 19
Test #9:
score: 0
Accepted
time: 3ms
memory: 3736kb
input:
ESYXKNQELMVKOHKSUJQHLPDUWFWNZRNPQQVHFYXBSYCHJPPBURRMFBKNQZKZMFITHOGXZQHCOVGWLSQABGRWWNOSEUDVSVGXHHDH
output:
3 110 +------------------------------------------------------------------------------------------------------+ S->+ ESYXKNQELMVKOHKSUJQHLPDUWFWNZRNPQQVHFYXBSYCHJPPBURRMFBKNQZKZMFITHOGXZQHCOVGWLSQABGRWWNOSEUDVSVGXHHDH +->F +--------------------------------------------------------------------...
result:
ok 3 110
Test #10:
score: 0
Accepted
time: 6ms
memory: 3556kb
input:
Q|W|R|K|E|O|R|I|V|E|Z|M|P|Y|U|I|K|E|X|V|L|A|G|F|M|L|I|W|R|Z|I|O|A|P|X|A|F|X|F|X|M|I|M|Y|X|Q|N|V|T|Y
output:
199 17 +---+ S->+->+ Q +->+->F | +---+ | | | | +---+ | +->+ W +->+ | +---+ | | | | +---+ | +->+ R +->+ | +---+ | | | | +---+ | +->+ K +->+ | +---+ | | | | ...
result:
ok 199 17
Test #11:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
(((((((((((((((((((((((((((((((((((((((((((((((((N)))))))))))))))))))))))))))))))))))))))))))))))))
output:
3 11 +---+ S->+ N +->F +---+
result:
ok 3 11
Test #12:
score: 0
Accepted
time: 3ms
memory: 3592kb
input:
INYWKMBDKYEFGBBONRCUUYMWAICQANQCECIVRHEKLLJEKAGJU|MLWORHGFUGRWAQGOXAXYDPMWJSODKPLRTOLCNGZXXVPUPGQYHU
output:
7 66 +---------------------------------------------------+ S->+->+ INYWKMBDKYEFGBBONRCUUYMWAICQANQCECIVRHEKLLJEKAGJU +-->+->F | +---------------------------------------------------+ | | | | +--------------------...
result:
ok 7 66
Test #13:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
(G|OFC|C|(DK))(RMOD|(V))|((S(Y)(JB)(HZTS|S|S|(M((KP(I|Q)(N)))(((JQ|(X|JH))))))GRY)|TS|F|((WRKS)|WP))
output:
55 108 +---+ +------+ S->+->+->+ G +--->+->+->+ RMOD +->+-------------------------------------------------------------------->+->F | | +---+ | | +------+ | ...
result:
ok 55 108
Test #14:
score: 0
Accepted
time: 5ms
memory: 3652kb
input:
(OS+|((VT(UZS)+))|U+|((F)|Z+++)+|((O)(R+|S+)+|T+++|FD|L)(O|Q+)++IFGX+(L|K|(TU(I)+)++)+HP(Q)+U)+|Q|M+
output:
77 186 +---+ +---+ S->+->+->+->+ O +->+->+ S +->+----------------------------------------------------------------------------...
result:
ok 77 186
Test #15:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
(((JU)?|(M)?|D(C|A|D)??X|P|H?|G|Y))(L?)(A?)S???((A|R?)??)((G|J)??)BQ((T))?(AXR??Z|D?Y((Z)((O))??)YW)
output:
50 237 +----+ +----+ +---+ S->+->+--------->+-------------------------->+->+------...
result:
ok 50 237
Test #16:
score: 0
Accepted
time: 5ms
memory: 3668kb
input:
AH|(TY|T|W)*|((W*|(X|(GV|A*FJ*S(Y)Y*)|H))**(((JE)***JUE|K|Z*|Z*)***(L***P((M|IB*)|(K|(O|Y))*|P)*)*))
output:
76 235 +----+ S->+->+ AH +---------------------------------------------...
result:
ok 76 235
Test #17:
score: 0
Accepted
time: 10ms
memory: 4140kb
input:
O+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
output:
201 605 ...
result:
ok 201 605
Test #18:
score: 0
Accepted
time: 21ms
memory: 4360kb
input:
V???????????????????????????????????????????????????????????????????????????????????????????????????
output:
300 605 ...
result:
ok 300 605
Test #19:
score: 0
Accepted
time: 38ms
memory: 4636kb
input:
S***************************************************************************************************
output:
498 605 ...
result:
ok 498 605
Test #20:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
NTN|J+|H|O++++++++++++++++++++++++++M+++++++++|F++++W+++++++++++++++|WF|J+++NAH+Z+++++GRXI++P+++++++
output:
125 234 +-----+ S->+->+ NTN +--------------------------------------------...
result:
ok 125 234
Test #21:
score: 0
Accepted
time: 11ms
memory: 4096kb
input:
G??????????????????????????????????????????????????????????????????????????????BI???????????????????
output:
237 607 ...
result:
ok 237 607
Test #22:
score: 0
Accepted
time: 3ms
memory: 4084kb
input:
B***|BFP*|G*******|U|K*|B|H***********A***W****H************************************************D*ER
output:
327 455 ...
result:
ok 327 455
Test #23:
score: 0
Accepted
time: 30ms
memory: 4408kb
input:
K+++++*+??++?*?*?+**?+***+++++*?*?*+*+*****+?*+**+???+?**?+***?+?+??+?**+???+?*****?+*?*+*?*++??++?*
output:
341 605 ...
result:
ok 341 605
Test #24:
score: 0
Accepted
time: 16ms
memory: 4452kb
input:
W*??+**?+*****?*+?+??+?+???*?*+*?*????+++????+?+**+??*++?+*?+*+??+?*?*+???*+???*+**?+???*?+?????+++?
output:
323 605 ...
result:
ok 323 605
Test #25:
score: 0
Accepted
time: 9ms
memory: 4080kb
input:
I+*+?+?*++*+*?*|QE?I|PR+**+???*??***?+++*?****+*?+*+|Q+*+*+*??*+*??+?++*?+*++**?*?????*+*+*?*N+U**+?
output:
302 295 ...
result:
ok 302 295
Test #26:
score: 0
Accepted
time: 6ms
memory: 3804kb
input:
(((M*+?A)*????((E(JY)??(BT))*+?+++++)??***++(N)++?*??((AN|A)+*++Y???*+)??L?HQNQX??(R(OO))+O+++E))??+
output:
62 434 ...
result:
ok 62 434
Test #27:
score: 0
Accepted
time: 8ms
memory: 3740kb
input:
(Q*|B|D(M)?PNT|H|U|H|D?++|(P***?)|OG|W|(C*+?)|(R+?*+**?*M++))****+|X*EM|Z|H|Z|(MM)|(T*MW)ESCFN?*?|A?
output:
189 120 S->+->+->+--------------------------------------------------------------------------------------------------->+->+->+->F | | | ...
result:
ok 189 120
Test #28:
score: 0
Accepted
time: 5ms
memory: 3768kb
input:
((HR+MJ|X++|OVHLR**++Q)+??*?|QI|B*+BGP|Q|TCRB?XTEG?)??((D|H|A?)*++*)*?(ARCIPKV?(NUZ*Z*R)WVEZ+)+|LIXW
output:
83 261 +--------+ +----+ +---+ +-----+ +---+ S->+->+------------------------...
result:
ok 83 261