hdu4782 Beautiful Soup (模拟)
http://acm.hdu.edu.cn/showproblem.php?pid=4782
2013成都区域赛全题PDF:http://acm.hdu.edu.cn/downloads/2013ChengduProblemSet.pdf
题意:就标签里的不改,标签外的单词之间隔一个空格,按照标签来缩进。(hdu上的样例有错,居然有个[pre],简直尿,记得无视它)
题解:模拟!
我是用getchar()读的,读到标签外的不是那3个特殊字符(/t /n 空格)的,就直接putchar输出;读到标签里的就先存一存(因为是结束标签的话,缩进会改变,而不读完这个标签是不知道这是不是结束标签的……)
然后换行和空格什么的用几个flag搞一搞。
1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<algorithm> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<queue> 12 using namespace std; 13 #define mz(array) memset(array, 0, sizeof(array)) 14 #define mf1(array) memset(array, -1, sizeof(array)) 15 #define minf(array) memset(array, 0x3f, sizeof(array)) 16 #define REP(i,n) for(i=0;i<(n);i++) 17 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 18 #define RD(x) scanf("%d",&x) 19 #define RD2(x,y) scanf("%d%d",&x,&y) 20 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 21 #define WN(x) printf("%d\n",x); 22 #define RE freopen("B.in","r",stdin) 23 #define WE freopen("huzhi.txt","w",stdout) 24 #define mp make_pair 25 #define pb push_back 26 #define pf push_front 27 #define ppf pop_front 28 #define ppb pop_back 29 typedef long long ll; 30 typedef unsigned long long ull; 31 32 const double pi=acos(-1.0); 33 const double eps=1e-10; 34 const int TAG=0; 35 const int TEXT=1; 36 const char SP=' '; 37 38 39 char s[1024*20 + 7]; 40 int l=0; 41 42 int space; 43 44 void huanhang(){ 45 int i; 46 puts(""); 47 REP(i,space)putchar(SP); 48 } 49 50 int main(){ 51 //RE; 52 int T,cas=1; 53 char c; 54 bool in, needspace; 55 int pre; 56 int i; 57 RD(T); 58 while(T--){ 59 if(cas!=1)puts(""); 60 printf("Case #%d:",cas++); 61 space=0; 62 in=0; 63 pre=TAG; 64 needspace=0; 65 l=0; 66 while(1){ 67 c=getchar(); 68 if(c=='<'){ 69 in=1; 70 } 71 if(in){ 72 s[l++]=c; 73 } 74 if(c=='>'){ 75 in=0; 76 if(l>=2 && s[l-2]!='/'){ 77 if(l>1 && s[1]!='/'){ 78 huanhang(); 79 space++; 80 }else{ 81 space--; 82 huanhang(); 83 } 84 }else huanhang(); 85 REP(i,l)putchar(s[i]); 86 if(l==7 && s[1]=='/' && s[2]=='h' && s[3]=='t'&&s[4]=='m'&&s[5]=='l')break; 87 l=0; 88 needspace=0; 89 pre=TAG; 90 }else if(!in){ 91 if(c!=' ' && c!='\t' && c!='\n'){ 92 if(pre==TAG)huanhang(); 93 pre=TEXT; 94 if(needspace){ 95 putchar(SP); 96 needspace=0; 97 } 98 putchar(c); 99 }else{ 100 //printf("[%c]",c); 101 if(pre==TEXT)needspace=1; 102 } 103 } 104 } 105 } 106 puts(""); 107 return 0; 108 }