标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1912 Accepted Submission(s): 391
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1e9+10; string s[20010]; int n; char pc;bool pcv; void get1(char c) { string t=""; while(c!=‘>‘){ t+=c; c=getchar(); } t+=c; s[++n]=t; pc=getchar(); pcv=1; } void get2(char c) { string t=""; while(c!=‘<‘){ t+=c; c=getchar(); if(c==‘ ‘||c==9||c==‘\n‘) c=‘ ‘,t+=c; while(c==‘ ‘||c==‘\n‘||c==9) c=getchar(); } int len=t.size(); while(t[len-1]==‘ ‘) t[len-1]=‘\0‘,len--; s[++n]=t; pc=c; pcv=1; } bool Get(char c) { pcv=0; while(c==‘ ‘||c==‘\n‘||c==9) c=getchar(); if(c==‘<‘) get1(c); else get2(c); if(s[n]=="</html>") return 1; return 0; } void input() { n=0; if(pcv==0) pc=getchar(); while(!Get(pc)); } void solve() { //REP(i,1,n) cout<<s[i]<<endl; int tag=0; REP(i,1,n){ if(s[i][0]==‘<‘&&s[i][1]==‘/‘) tag--; REP(j,1,tag) printf(" "); cout<<s[i]<<‘\n‘; int len=s[i].size(); if(s[i][0]==‘<‘&&s[i][1]!=‘/‘&&(len<2||(len>=2&&s[i][len-2]!=‘/‘))) tag++; } } int main() { freopen("in.txt","r",stdin); int T;cin>>T;int casen=1; pc=‘#‘; pcv=0; while(T--){ printf("Case #%d:\n",casen++); input(); solve(); } return 0; } /** 2 <html> <body> <h1> ACM ICPC </h1> <p> Hello <br/> World </p> </body></html> <html> <body> <p> Asia Chengdu Regional</p> <p class = "icpc"> ACM-ICPC </p> </body></html> */
下面是按其他人的思路重写的AC代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1e9+10; string s[20010]; int n; void input(char &pc,bool pvc) { n=0; char c=pc; if(pvc==0) c=getchar(); while(1){ string t=""; while(c==‘\n‘||c==‘ ‘||c==9) c=getchar(); if(c==‘<‘){/// 读类标签 while(c!=‘>‘) t+=c,c=getchar(); t+=c;c=getchar(); } else{/// 读一般串 while(c!=‘ ‘&&c!=‘\n‘&&c!=9&&c!=‘<‘) t+=c,c=getchar(); } s[++n]=t; pc=c; if(t=="</html>") return; } } void solve() { //REP(i,1,n) cout<<s[i]<<endl; int tag=0; int last=0,first=1; REP(i,1,n){ string t=s[i]; int len=t.size(); if(t[0]==‘<‘){ /// 是类标签 if(t[1]==‘/‘){ /// 是结束标签 tag--; } REP(j,1,tag) cout<<" "; cout<<t<<endl; if(t[1]!=‘/‘&&t[len-2]!=‘/‘){/// 是开始标签 tag++; } first=1; } else{///是一般串 if(s[i+1][0]==‘<‘) last=1; else last=0; if(first) REP(j,1,tag) cout<<" "; if(first) first=0; cout<<t; if(last){/// 是尾串 cout<<endl; } else cout<<" "; } } } int main() { freopen("in.txt","r",stdin); int T;cin>>T;int casen=1; char pc=‘#‘;bool pvc=0; while(T--){ printf("Case #%d:\n",casen++); input(pc,pvc); solve(); pvc=1; } return 0; } /** 2 <html> <body> <h1> ACM ICPC </h1> <p> Hello <br/> World </p> </body></html> <html> <body> <p> Asia Chengdu Regional</p> <p class = "icpc"> ACM-ICPC </p> </body></html> */
如果比赛中碰到这种难度的模拟,30分钟确实足够了,再加上读题40分钟,当然这种应该和计算几何并列放在其它题之后,神题之前做。
对我而言这种模拟题是确实是永远无法稳过的,但是我还是会补这种题,慢慢增大过这种题的概率,如果平时不补,那么比赛的时候就只能靠奇迹了,相比奇迹,我更相信概率。
标签:
原文地址:http://www.cnblogs.com/--560/p/5250711.html