首页 > 代码库 > hdu - 4782 - Beautiful Soup(模拟)
hdu - 4782 - Beautiful Soup(模拟)
[cpp] view plaincopy
- #include <cstdio>
- #include <cstring>
- const int MAXN = 200;
- const char* stop = "</html>";
- char ch;
- bool IsSpace(char ch)
- {
- return ch == 32 || ch == 9 || ch == 10;
- }
- void PrintSpace(int n)
- {
- while (n--)
- {
- putchar(‘ ‘);
- }
- }
- void RemoveSpace()
- {
- while ((ch = getchar()) && IsSpace(ch));
- }
- void Enter()
- {
- putchar(‘\n‘);
- }
- void GetEntireTag(char* tag)
- {
- int len = 0;
- tag[len++] = ‘<‘;
- while ((ch = getchar()) && ch != ‘>‘)
- {
- tag[len++] = ch;
- }
- tag[len++] = ‘>‘;
- tag[len] = ‘\0‘;
- }
- void OutputTag(const char* tag, const int& spaceCnt)
- {
- if (tag[1] == ‘/‘)
- {
- PrintSpace(spaceCnt - 1);
- }
- else
- {
- PrintSpace(spaceCnt);
- }
- puts(tag);
- }
- void UpdateSpace(const char* tag, int& spaceCnt)
- {
- int len = strlen(tag);
- if (tag[1] != ‘/‘ && tag[len - 2] != ‘/‘)
- {
- ++spaceCnt;
- }
- else if (tag[1] == ‘/‘)
- {
- --spaceCnt;
- }
- }
- void GetAndOutputEntireText(const int& spaceCnt)
- {
- PrintSpace(spaceCnt);
- putchar(ch);
- while ((ch = getchar()) && ch != ‘<‘)
- {
- if (IsSpace(ch))
- {
- RemoveSpace();
- if (ch == ‘<‘) break;
- else
- {
- PrintSpace(1);
- putchar(ch);
- }
- }
- else
- {
- putchar(ch);
- }
- }
- Enter();
- }
- int main()
- {
- int T, kase = 0;
- char tag[MAXN];
- scanf("%d", &T);
- getchar();
- while (T--)
- {
- int spaceCnt = 0;
- ch = getchar();
- printf("Case #%d:\n", ++kase);
- while (true)
- {
- if (IsSpace(ch))
- {
- RemoveSpace();
- }
- else if (ch == ‘<‘)
- {
- GetEntireTag(tag);
- OutputTag(tag, spaceCnt);
- if (strcmp(tag, stop) == 0) break;
- UpdateSpace(tag, spaceCnt);
- ch = getchar();
- }
- else
- {
- GetAndOutputEntireText(spaceCnt);
- }
- }
- }
- return 0;
- }
hdu - 4782 - Beautiful Soup(模拟)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。