AcWing 3244. Markdown-csp10(3)
原题链接
中等
作者:
YAX_AC
,
2024-11-20 18:22:26
,
所有人可见
,
阅读 4
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<string> strs;
int work_link(string str,int i)
{
string text,link;
for(i++; str[i] != ']'; i++)
{
char c = str[i];
if(c == '_')
{
text += "<em>";
i++;
while(str[i] != '_') text += str[i++];
text += "</em>";
}
else text += c;
}
for(i+=2; str[i] != ')'; i++) link += str[i];
printf("<a href=\"%s\">%s</a>",link.c_str(),text.c_str());
return i;
}
int work_em(string str,int i)
{
printf("<em>");
for(i++; str[i]!='_'; i++)
{
char c = str[i];
if(c=='[') i = work_link(str,i);
else cout<<c;
}
printf("</em>");
return i;
}
void work_line(string str)
{
int k = 0;
while(str[k] == ' ') k++;
str = str.substr(k);
for(int i = 0; i<str.size(); i++)
{
char c = str[i];
if(c == '_') i = work_em(str,i);
else if(c == '[') i = work_link(str,i);
else cout<<c;
}
}
void work(int a,int b)
{
if(strs[a][0] == '#')
{
int k = 0;
while(strs[a][k] == '#') k++;
printf("<h%d>",k);
work_line(strs[a].substr(k));
printf("</h%d>\n",k);
}
else if(strs[a][0] == '*')
{
printf("<ul>\n");
for(int i = a; i<=b; i++)
{
printf("<li>");
work_line(strs[i].substr(1));
printf("</li>\n");
}
printf("</ul>\n");
}
else
{
printf("<p>");
for(int i = a; i<=b; i++)
{
work_line(strs[i]);
if(i!=b)
cout<<endl;
}
printf("</p>\n");
}
}
int main()
{
string str;
while(getline(cin,str)) strs.push_back(str);
for(int i = 0; i<strs.size(); i++)
{
if(strs[i].empty()) continue;
int j = i+1;
while(j<strs.size() && strs[j].size()) j++;
work(i,j-1);
i = j-1;
}
return 0;
}