题目给出了一种简化html语言的方法,然后告诉你简化后的句子,让你翻译成原来的句子。
模拟..就是翻译就可以了...
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
inline int toInt(const string &s) {
int ans=0;
for (int i=0;i<s.length();i++) {
ans=ans*10+s[i]-'0';
}
return ans;
}
struct Tag {
string name,idName,className;
Tag(string s,int ×) {
int i=0;
times=1;
while (i<s.size()&&s[i]!='.'&&s[i]!='#'&&s[i]!='*') i++;
name=s.substr(0,i);
while (i<s.size()) {
int from=i++;
while (i<s.size()&&s[i]!='.'&&s[i]!='#'&&s[i]!='*') i++;
if (s[from]=='#') {
idName=s.substr(from+1,i-from-1);
} else if (s[from]=='.') {
if (className=="") className=s.substr(from+1,i-from-1);
else className+=" "+s.substr(from+1,i-from-1);
} else if (s[from]=='*') {
times*=toInt(s.substr(from+1,i-from-1));
}
}
}
string start() {
string ans("<");
ans+=name;
if (idName!="")
ans+=" id=\""+idName+"\"";
if (className!="")
ans+=" class=\""+className+"\"";
ans+=">";
return ans;
}
string end() {
string ans("</");
ans+=name;
ans+=">";
return ans;
}
};
string trans(const string &s) {
if (s.size()==0) return string("");
//cout << "trans:: " << s << endl;
if (s[0]=='(') {
int i=1,num=1,lasts=-1;
while (num) {
if (s[i]==')') num--;
else if (s[i]=='(') num++;
i++;
}
while (i<s.size()&&s[i]=='*') {
lasts=i;
i++;
while (s[i]>='0'&&s[i]<='9') i++;
}
if (lasts==-1) {
return trans(s.substr(1,i-2))+trans(s.substr(i));
} else {
string ans;
int times=toInt(s.substr(lasts+1,i-lasts-1));
string tmp=trans(s.substr(0,lasts));
while (times--) ans+=tmp;
return ans+trans(s.substr(i));
}
} else {
int i=0,times;
while (i<s.size()&&s[i]!='>') i++;
Tag t=Tag(s.substr(0,i),times);
string tmp=t.start();
if (i<s.size()) tmp+=trans(s.substr(i+1));
tmp+=t.end();
string ans;
while (times--) ans+=tmp;
return ans;
}
}
int main() {
int t;
string s;
cin >> t;
while (t--) {
cin >> s;
s=trans(s);
cout << s << endl;
}
return 0;
}