AcWing 3244. Markdown
原题链接
中等
作者:
把这题Ac了
,
2024-12-01 15:16:56
,
所有人可见
,
阅读 2
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<string> s;
int work_link(string str,int i){
string link,text;
for(i++;str[i] != ']';i++){
char c = str[i];
if(c == '_'){
i++;
text += "<em>";
while(i < str.size() && str[i] != '_') text += str[i++];
text += "</em>";
}
else text += c;
}
i += 2;
while(i < str.size() && str[i] != ')'){
char c = str[i++];
link += c;
}
// <a href="Link">Text</a>
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 j = 0;
while(j < str.size() && str[j] == ' ') j++;
str = str.substr(j);
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;
}
}
//work处理区块
void work(int a,int b){
if(s[a][0] == '#'){
int j = 0;
while(j < s[a].size() && s[a][j] == '#') j++;
printf("<h%d>",j);
work_line(s[a].substr(j));
printf("</h%d>\n",j);
}else if(s[a][0] == '*'){
printf("<ul>\n");
for(int i = a;i <= b;i++){
printf("<li>");
work_line(s[i].substr(1));
printf("</li>\n");
}
printf("</ul>\n");
}else{
printf("<p>");
for(int i = a;i <= b;i++){
work_line(s[i]);
if(i != b) puts("");
}
printf("</p>\n");
}
}
int main(){
string str;
while(getline(cin,str)) s.push_back(str);
for(int i = 0;i < s.size();i++){
string t = s[i];
if(!t.size()) continue;
int j = i + 1;
//找到下个空行
while(j < s.size() && s[j].size()) j++;
work(i,j - 1);
i = j - 1;
}
return 0;
}