您的位置:首页 > 其它

HDU 4782 Beautiful Soup 模拟

2014-08-11 19:35 309 查看
【思路】

以前遇到这种题,老是想用dfs来写,现在终于知道用栈模拟更方便了 =  =。模拟题,没什么好说的,读懂题意剩下的就是码力的问题了。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF = 100011122;
const double INFF = 1e100;
const double eps = 1e-8;
const int mod = 1000000009;
const int NN = 110;
const int MM = 5000010;
/* ****************** */

void FF_kong(int x)
{
while(x)
{
printf(" ");
x--;
}
}

int main()
{
char c;
int cas, ee = 0;
int tail = 0;
string ss;
bool tag = true;
bool need = true;
bool first;
scanf("%d",&cas);
while(1)
{
if(tag)
{
while(need && (c = getchar()) )
{
if(c=='<')
break;
}
ss = "";
while( (c = getchar()) )
{
if(c=='>')
break;
ss += c;
}

if(ss=="html")
{
printf("Case #%d:\n",++ee);
}

if(ss[ss.size()-1]=='/')
{
FF_kong(tail);
cout<<"<"<<ss<<">"<<endl;
tag = false;

}
else
{
if(ss[0]=='/')
{
tail--;
FF_kong(tail);
cout<<"<"<<ss<<">"<<endl;
tag = false;

}
else
{
FF_kong(tail);
cout<<"<"<<ss<<">"<<endl;
tag = false;
tail++;

}
}

if(ss=="/html" && ee==cas)
break;

ss = "";
}
else
{
first = true;
while( (c = getchar()) )
{
if(c=='<')
{
if(first)
{
if(ss.size())
{
FF_kong(tail);
first = false;
cout<<ss<<endl;
ss = "";
}
}
else
{
if(ss.size())
cout<<" "<<ss;
cout<<endl;
ss = "";
}
need = false;
tag = true;
break;
}

if(c==32 || c==9 || c==10)
{
if(ss.size())
{
if(first)
{
FF_kong(tail);
first = false;
cout<<ss;
}
else
{
cout<<" "<<ss;
}
}
ss = "";
}
else
ss += c;
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: