HTLM
实现扩展括号匹配算法,用来检查HTML文档的标记是否匹配。HTML标记应该成对、嵌套出现,开标记是<tag>
这种形式,闭标记是</tag>
这种形式。
输入样例:
<html> <head> <title>Example</title> </head> <body> <h1>Hello, world</h1> </body> </html>
输出样例:
True
#include <cstring>
#include <string>
#include <cmath>
#include <iomanip>
#include <stack>
using namespace std;
stack<string> s;
int main()
{
int flag = 1;
char c;
while (cin >> c && c != EOF && flag == 1)
{
if (c == '<')
{
char next;
char opent[100];
char closet[100];
string open;
string close;
cin >> next;
if (next != '/')
{
cin.putback(next);
cin.getline(opent, 100, '>');
open = opent;
s.push(open);
}
else
{
if (s.empty())
flag = 0;
else
{
cin.getline(closet, 100, '>');
close = closet;
open = s.top();
s.pop();
if (open != close)
flag = 0;
}
}
}
else
continue;
}
if (flag == 0)
cout << "False" << endl;
else
cout << "True" << endl;
return 0;
}