您的位置:首页 > 其它

HDU 4782 Beautiful Soup (模拟+注意细节)

2013-11-17 20:46 423 查看
思路就是用栈模拟,不用开实体的栈,直接记一个top指针就行。

说说这题的细节:

1.tag标签里的内容不要动,原样输出。比如<p aa bb cc>,就这样输出就行,不要删空格。题目中说了you shouldn’t change anything of any tag.

2.标签之外的文本,文本内的空白符(空格,tab,回车),相邻单词之间用一个空格分隔开。文本与标签相邻的地方,不要有多余的空白符,就是说,文本与标签相邻的地方,除了一个回车以及缩进用的空白符之外,不要有任何空白符。

3.上一个case的</html>跟下一个case的<html>有可能在同一行,并且第一个<html>之前有可能有空白符

4.每行结尾不要有多余的空格

5.不要有空行

以下给出几组数据,空格用<SPACE>表示,tab键用<TAB>表示:

Input:

5
<TAB><html>                <body>
<h1>ACM
ICPC</h1>
<p>Hello<br/>World</p>
</body></html>
<html>            <body><p><TAB>
Asia Chengdu Regional        </p><TAB>
<p class="icpc">
ACM-ICPC</p></body></html>
<html>          <TAB>
<TAB>
</html><TAB><html>
<p>
<TAB>
aa bb          cc

dafdadgsdfsa<TAB>

afd
</p>
<TAB>   </html><TAB><TAB>
<html><body aslfja        fdsafs<TAB>fdsafsa  ><bb/></body></html>


Output

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>
Case #3:
<html>
</html>
Case #4:
<html>
<p>
aa bb cc dafdadgsdfsa afd
</p>
</html>
Case #5:
<html>
<body aslfja        fdsafs    fdsafsa  >
<bb/>
</body>
</html>


代码

#include <cstdio>
#include <cstring>
#include <cstdlib>

#define IN 0
#define OUT 1

#define START 0
#define END 1

using namespace std;

const int MAXN = 210000;

char str[MAXN];
char tmp[MAXN];
char tag[MAXN];

bool CheckEnd( char *s, int len )
{
if ( s[len-6] == '<' && s[len-5] == '/' && s[len-4] == 'h'
&& s[len-3] == 't' && s[len-2] == 'm' && s[len-1] == 'l'
&& s[len] == '>' )
return true;
return false;
}

int chuli( char *s, int len )
{
int i = 0, j = 0;
while ( i < len && (s[i] == ' ' || s[i] == 9 || s[i] == '\n') ) ++i;
while ( i < len )
{
s[j] = s[i];
if ( s[i] == '>' )
{
++i;
while ( s[i] == ' ' || s[i] == 9 || s[i] == '\n' ) ++i;
}
else ++i;
++j;
}
s[j] = '\0';
return j;
}

int DeleteSpace( char *s, int len )
{
int i = 0, j = 0;
while ( i < len )
{
if ( s[i] == '<' )
{
if ( j - 1 >= 0 && ( s[j - 1] == ' ' || s[j - 1] == 9 ) ) s[j - 1] = s[i++];
while ( i < len && s[i] != '>' )
{
s[j++] = s[i++];
}
s[j++] = s[i++];
}
else if ( s[i] == ' ' || s[i] == 9 )
{
s[j++] = ' ';
while ( i < len && ( s[i] == ' ' || s[i] == 9 ) ) ++i;
}
else s[j++] = s[i++];
}
s[j] = '\0';
return j;
}

void solved( char* s, int len )
{
//puts(s);
int top = 0;
int i = 0;
while ( i < len )
{
if ( s[i] == '<' )
{
int j = 0;
while ( s[i] != '>' )
{
tag[j++] = s[i++];
}
tag[j++] = s[i++];
tag[j] = '\0';

if ( tag[1] == '/' )
{
--top;
for ( int k = 0; k < top; ++k )
putchar(' ');
}
else
{
for ( int k = 0; k < top; ++k )
putchar(' ');
++top;
}
if ( tag[j-2] == '/' ) --top;
puts(tag);
}
else
{
for ( int k = 0; k < top; ++k ) putchar(' ');
while ( i < len && s[i] != '<' )
{
putchar(s[i]);
++i;
}
puts("");
}
}
return;
}

int main()
{
int T, cas = 0;
//freopen( "in.txt", "r", stdin );
//freopen( "s.txt", "w", stdout );
scanf( "%d", &T );
printf( "Case #%d:\n", ++cas );
int len = 0;
while ( gets(tmp) != NULL )
{
strcpy( &str[len], tmp );
len += strlen(tmp);
str[len++] = ' ';
}
str[len - 1] = '\0';
//puts(str);

int j = 0;
for ( int i = 0; i < len; )
{
tmp[j++] = str[i++];
if ( j > 6 && CheckEnd( tmp, j - 1 ) )
{
tmp[j] = '\0';
--T;
//printf( "T = %d\n", T );
solved( tmp, DeleteSpace( tmp, chuli( tmp, j ) ) );
j = 0;
if ( T ) printf( "Case #%d:\n", ++cas );
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: