Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有限状态机+递归#include <cstdio> #include <cctype> void newline(int spaces) { if(spaces > 0) putchar('\n'); for(int i = 0; i < spaces; ++i) putchar(' '); } void inout(int level) { newline(level << 2); int c; bool afterPeriod = false; while((c = getchar()) != -1){ if(isspace(c)) continue; if(c == '{'){ if(level) putchar(' '); putchar('{'); inout(level+1); //start a new level } else if(c == ';'){ putchar(';'); afterPeriod = true; } else if(c == '}'){ if(level > 1) newline((level-1) << 2); else putchar('\n'); putchar('}'); //last level's end return; } else if(c == ','){ putchar(','); putchar(' '); } else{ if(afterPeriod){ newline(level << 2); afterPeriod = false; } putchar(c); } } } int main() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); inout(0); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator