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搞一搞。

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("B.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
typedef long long ll;
typedef unsigned long long ull; const double pi=acos(-1.0);
const double eps=1e-;
const int TAG=;
const int TEXT=;
const char SP=' '; char s[* + ];
int l=; int space; void huanhang(){
int i;
puts("");
REP(i,space)putchar(SP);
} int main(){
//RE;
int T,cas=;
char c;
bool in, needspace;
int pre;
int i;
RD(T);
while(T--){
if(cas!=)puts("");
printf("Case #%d:",cas++);
space=;
in=;
pre=TAG;
needspace=;
l=;
while(){
c=getchar();
if(c=='<'){
in=;
}
if(in){
s[l++]=c;
}
if(c=='>'){
in=;
if(l>= && s[l-]!='/'){
if(l> && s[]!='/'){
huanhang();
space++;
}else{
space--;
huanhang();
}
}else huanhang();
REP(i,l)putchar(s[i]);
if(l== && s[]=='/' && s[]=='h' && s[]=='t'&&s[]=='m'&&s[]=='l')break;
l=;
needspace=;
pre=TAG;
}else if(!in){
if(c!=' ' && c!='\t' && c!='\n'){
if(pre==TAG)huanhang();
pre=TEXT;
if(needspace){
putchar(SP);
needspace=;
}
putchar(c);
}else{
//printf("[%c]",c);
if(pre==TEXT)needspace=;
}
}
}
}
puts("");
return ;
}
05-11 17:06
查看更多