QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#772919#1662. ASCII Automata ArtI_be_wannaAC ✓17ms4620kbC++176.9kb2024-11-22 22:57:012024-11-22 22:57:02

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 22:57:02]
  • 评测
  • 测评结果:AC
  • 用时:17ms
  • 内存:4620kb
  • [2024-11-22 22:57:01]
  • 提交

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;
	}
}
/*#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,'-'));
}*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3664kb

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: 3872kb

input:

N(ER)C

output:

3 26
   +---+  +----+  +---+   
S->+ N +->+ ER +->+ C +->F
   +---+  +----+  +---+   

result:

ok 3 26

Test #3:

score: 0
Accepted
time: 1ms
memory: 3536kb

input:

C|ON|TEST

output:

11 20
      +---+         
S->+->+ C +---->+->F
   |  +---+     |   
   |            |   
   |  +----+    |   
   +->+ ON +--->+   
   |  +----+    |   
   |            |   
   |  +------+  |   
   +->+ TEST +->+   
      +------+      

result:

ok 11 20

Test #4:

score: 0
Accepted
time: 1ms
memory: 3580kb

input:

A+

output:

5 17
      +---+      
S->+->+ A +->+->F
   |  +---+  |   
   |         |   
   +<--------+   

result:

ok 5 17

Test #5:

score: 0
Accepted
time: 1ms
memory: 3612kb

input:

B?

output:

6 17
                 
S->+-------->+->F
   |         |   
   |  +---+  |   
   +->+ B +->+   
      +---+      

result:

ok 6 17

Test #6:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

C*

output:

8 17
                 
S->+-------->+->F
   |         |   
   |  +---+  |   
   +->+ C +->+   
   |  +---+  |   
   |         |   
   +<--------+   

result:

ok 8 17

Test #7:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

Z

output:

3 11
   +---+   
S->+ Z +->F
   +---+   

result:

ok 3 11

Test #8:

score: 0
Accepted
time: 1ms
memory: 3876kb

input:

((((DEF)|(GH)|(I))))

output:

11 19
      +-----+      
S->+->+ DEF +->+->F
   |  +-----+  |   
   |           |   
   |  +----+   |   
   +->+ GH +-->+   
   |  +----+   |   
   |           |   
   |  +---+    |   
   +->+ I +--->+   
      +---+        

result:

ok 11 19

Test #9:

score: 0
Accepted
time: 1ms
memory: 3608kb

input:

ESYXKNQELMVKOHKSUJQHLPDUWFWNZRNPQQVHFYXBSYCHJPPBURRMFBKNQZKZMFITHOGXZQHCOVGWLSQABGRWWNOSEUDVSVGXHHDH

output:

3 110
   +------------------------------------------------------------------------------------------------------+   
S->+ ESYXKNQELMVKOHKSUJQHLPDUWFWNZRNPQQVHFYXBSYCHJPPBURRMFBKNQZKZMFITHOGXZQHCOVGWLSQABGRWWNOSEUDVSVGXHHDH +->F
   +--------------------------------------------------------------------...

result:

ok 3 110

Test #10:

score: 0
Accepted
time: 0ms
memory: 3676kb

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: 3640kb

input:

(((((((((((((((((((((((((((((((((((((((((((((((((N)))))))))))))))))))))))))))))))))))))))))))))))))

output:

3 11
   +---+   
S->+ N +->F
   +---+   

result:

ok 3 11

Test #12:

score: 0
Accepted
time: 1ms
memory: 3640kb

input:

INYWKMBDKYEFGBBONRCUUYMWAICQANQCECIVRHEKLLJEKAGJU|MLWORHGFUGRWAQGOXAXYDPMWJSODKPLRTOLCNGZXXVPUPGQYHU

output:

7 66
      +---------------------------------------------------+       
S->+->+ INYWKMBDKYEFGBBONRCUUYMWAICQANQCECIVRHEKLLJEKAGJU +-->+->F
   |  +---------------------------------------------------+   |   
   |                                                          |   
   |  +--------------------...

result:

ok 7 66

Test #13:

score: 0
Accepted
time: 1ms
memory: 3644kb

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: 1ms
memory: 3908kb

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: 1ms
memory: 3704kb

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: 0ms
memory: 3660kb

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: 8ms
memory: 4396kb

input:

O+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

output:

201 605
                                                                                                                                                                                                                                                                                                    ...

result:

ok 201 605

Test #18:

score: 0
Accepted
time: 8ms
memory: 4492kb

input:

V???????????????????????????????????????????????????????????????????????????????????????????????????

output:

300 605
                                                                                                                                                                                                                                                                                                    ...

result:

ok 300 605

Test #19:

score: 0
Accepted
time: 17ms
memory: 4620kb

input:

S***************************************************************************************************

output:

498 605
                                                                                                                                                                                                                                                                                                    ...

result:

ok 498 605

Test #20:

score: 0
Accepted
time: 2ms
memory: 3784kb

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: 8ms
memory: 4204kb

input:

G??????????????????????????????????????????????????????????????????????????????BI???????????????????

output:

237 607
                                                                                                                                                                                                                                                                                                    ...

result:

ok 237 607

Test #22:

score: 0
Accepted
time: 2ms
memory: 4104kb

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: 14ms
memory: 4616kb

input:

K+++++*+??++?*?*?+**?+***+++++*?*?*+*+*****+?*+**+???+?**?+***?+?+??+?**+???+?*****?+*?*+*?*++??++?*

output:

341 605
                                                                                                                                                                                                                                                                                                    ...

result:

ok 341 605

Test #24:

score: 0
Accepted
time: 13ms
memory: 4528kb

input:

W*??+**?+*****?*+?+??+?+???*?*+*?*????+++????+?+**+??*++?+*?+*+??+?*?*+???*+???*+**?+???*?+?????+++?

output:

323 605
                                                                                                                                                                                                                                                                                                    ...

result:

ok 323 605

Test #25:

score: 0
Accepted
time: 3ms
memory: 3928kb

input:

I+*+?+?*++*+*?*|QE?I|PR+**+???*??***?+++*?****+*?+*+|Q+*+*+*??*+*??+?++*?+*++**?*?????*+*+*?*N+U**+?

output:

302 295
                                                                                                                                                                                                                                                                                                    ...

result:

ok 302 295

Test #26:

score: 0
Accepted
time: 1ms
memory: 4028kb

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: 0ms
memory: 3952kb

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: 1ms
memory: 3776kb

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