CCF201703-3
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void out(string s) {
cout << s << endl;
}
void outString(vector<string> res) {
for_each(res.begin(), res.end(), out);
}
void process_h(vector<string>& res, int& i, vector<string>& ans) {
int j = 0;
while (res[i][j] == '#') {
j++;
}
int k = j;
while (res[i][j] == ' ') {
j++;
}
char c = k + '0';
string tmp(1, c);
string subr;
subr.assign(res[i], j, res[i].npos);
string a = "<h" + tmp + ">" + subr + "</h" + tmp + ">";
ans.push_back(a);
}
void process_p(vector<string>& res, int& i, vector<string>& ans) {
int len = res.size();
int j = i;
while (res[i].size() != 0 && (res[i][0] != '#' && res[i][0] != '*')) {
i++;
if (i == len)
break;
}
i = i - 1;
res[j] = "<p>" + res[j];
res[i] = res[i] + "</p>";
for (int x = j;x <= i;x++) {
ans.push_back(res[x]);
}
}
void process_ul(vector<string>& res, int& i, vector<string>& ans) {
string front("<ul>");
string behind("</ul>");
int len = res.size();
ans.push_back(front);
while (res[i].size() != 0 && res[i][0] == '*') {
int k = 1;
while (res[i][k] == ' ') {
k++;
}
string subr;
subr.assign(res[i], k, res[i].npos);
res[i] = "<li>" + subr + "</li>";
ans.push_back(res[i]);
i++;
if (i == len)
break;
}
ans.push_back(behind);
i = i - 1;
}
void process(vector<string>& ans, int index) {
int size = ans[index].size();
int flag = 0;
string res;
for (int j = 0;j < size;j++) {
if (flag == 0 && ans[index][j] == '_') {
res = res + "<em>";
flag = 1;
}
else if (flag == 1 && ans[index][j] == '_') {
res = res + "</em>";
flag = 0;
}
else if (ans[index][j] == '[') {
int k = j;
while (ans[index][j] != ']') {
j++;
}
string text;
text.assign(ans[index], k + 1, j - k - 1);
int start = j + 2;
while (ans[index][j] != ')') {
j++;
}
string link;
link.assign(ans[index], start, j - start);
string tmp = "<a href=\"" + link + "\">" + text + "</a>";
res = res + tmp;
}
else {
string tmp(1, ans[index][j]);
res = res + tmp;
}
}
ans[index] = res;
}
void func() {
string line;
vector<string> res;
vector<string> ans;
while (getline(cin, line)) {
res.push_back(line);
}
int len = res.size();
for (int i = 0;i < len;i++) {
int size = res[i].size();
if (size != 0) {
if (res[i][0] == '#') {
process_h(res, i, ans);
}
else if (res[i][0] == '*') {
process_ul(res, i, ans);
}
else {
process_p(res, i, ans);
}
}
}
int len1 = ans.size();
for (int i = 0;i < len1;i++) {
process(ans, i);
process(ans, i);
}
outString(ans);
}
int main() {
func();
return 0;
}