给定语句,按照语法翻译html并输出。
就是恶心的模拟,递归搞就行了
处理id和class时,在一个'>'内,先把遇到的id和class都push到一个容器中,然后再输出即可。优先输出id,然后是class
递归过程即为分解成head+context+end的样子
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define RD(x) scanf("%d",&x)
using namespace std;
typedef pair<string, string> p;
#define MP make_pair
#define PB push_back
p tag(string a) {
if (a == "")
return MP("", "");
string x, y;
string b;
string name;
vector<string> id;
vector<string> classname;
int which = 0;
int i;
for (i = 0; i < a.length(); i++) {
if (a[i] == '.') {
if (which == 0)
name = b;
else if (which == 1)
id.PB(b);
else
classname.PB(b);
b = "";
which = 2;
} else if (a[i] == '#') {
if (which == 0)
name = b;
else if (which == 1)
id.PB(b);
else
classname.PB(b);
b = "";
which = 1;
} else {
b += a[i];
}
}
if (which == 0)
name = b;
else if (which == 1)
id.PB(b);
else
classname.PB(b);
b = "";
x = "<" + name;
if (id.size() != 0) {
x += " id=\"";
for (int i = 0; i < id.size(); i++) {
if (i)
x += " ";
x += id[i];
}
x += "\"";
}
if (classname.size() != 0) {
x += " class=\"";
for (int i = 0; i < classname.size(); i++) {
if (i)
x += " ";
x += classname[i];
}
x += "\"";
}
x += ">";
y = "</" + name + ">";
return MP(x, y);
}
string work(string a) {
if (a[0] == '(') {
int x = 1, p;
for (p = 1; p < a.length(); p++) {
if (a[p] == '(')
x++;
if (a[p] == ')')
x--;
if (x == 0)
break;
}
string a1 = a.substr(1, p - 1);
string a2 = a.substr(p + 1);
return work(a1 + '>') + work(a2);
}
int q = a.find('>');
if (q == -1)
return "";
string s = work(a.substr(q + 1));
a = a.substr(0, q);
int nn = 1;
if (a.find('*') != -1) {
int p = a.find('*');
sscanf(a.substr(p + 1).c_str(), "%d", &nn);
a = a.substr(0, p);
}
p tmp = tag(a);
string ret = tmp.first + s + tmp.second;
string ans = "";
for (int i = 0; i < nn; i++)
ans += ret;
return ans;
}
char c[400];
int main() {
int _;RD(_);
while (_--){
scanf("%s", c);
puts(work((string)c + '>').c_str());
}
return 0;
}