题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=4782
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>
[pre]Case #1: <html> <body> <h1> ACM ICPC </h1> <p> Hello <br/> World </p> </body> </html> Case #2: <html> <body> <p> Asia Chengdu Regional </p> <p class="icpc"> ACM-ICPC </p> </body> </html> [/pre]HintPlease be careful of the number of leading spaces of each line in above sample output.
题意:
输出一堆乱排版的<html>标签。去多余空字符,转换为按缩进输出。
代码例如以下:
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
vector<string> vv;
int main()
{
int t;
int cont, k;
int cas = 0;
char ss[1017];
char c;
scanf("%d",&t);
while(t--)
{
memset(ss,'\0',sizeof(ss));
cont = k = 0;
vv.clear();
c = getchar();
while(1)
{
while(c==' ' || c=='\n' || c=='\t')
c = getchar();
if(c != '<')
{
// ss[k++] = c;
// if(c == '>')
// c = getchar();
while(c!='<'&&c!='\n'&&c!='\t'&&c!=' ')
{
ss[k++] = c;
c = getchar();
}
ss[k] = '\0';
vv.push_back(ss);
k = 0;
//printf("ss1:%s\n",ss);
}
else
{
ss[k++] = '<';
while(c != '>')
{
c = getchar();
ss[k++] = c;
}
ss[k] = '\0';
vv.push_back(ss);
k = 0;
if(strcmp(ss,"</html>") == 0)
break;
c = getchar();
//printf("ss2:%s\n",ss);
}
}
// for(int i = 0; i < vv.size(); i++)
// {
// cout<<vv[i]<<endl;
// }
printf("Case #%d:\n",++cas);
int flag = 0;
for(int i = 0; i < vv.size(); i++)
{
if(vv[i][0] == '<')
{
flag = 0;
if(vv[i][1] != '/')//打开标签
{
for(int j = 0; j < cont; j++)
{
printf(" ");
}
cout<<vv[i]<<endl;
int len = vv[i].size();
if(vv[i][len-2]!='/')//不是关闭标签
cont++;
}
else//关闭标签
{
cont--;
for(int j = 0; j < cont; j++)
{
printf(" ");
}
cout<<vv[i]<<endl;
}
}
else if(!flag)
{
for(int j = 0; j < cont; j++)
{
printf(" ");
}
cout<<vv[i];
flag = 1;
if(vv[i+1][0] == '<')
printf("\n");
}
else if(flag)
{
printf(" ");
cout<<vv[i];
if(vv[i+1][0] == '<')
printf("\n");
}
}
}
return 0;
}原文:http://www.cnblogs.com/clnchanpin/p/7043662.html