# include<stdio.h>
# include<algorithm>
# include<string>
# include<vector>
# include<sstream>
# include<iostream>
using namespace std;
void check(string &t)
{//用来强调替换
int pos = t.find("_");
while(pos!=string::npos)
{
t.replace(pos,1,"<em>");
int pos_right = t.find("_");
t.replace(pos_right,1,"</em>");
pos = t.find("_");
}
}
void superlink(string &str)
{
int pos_link = str.find("](");
//如果有超链接
while(pos_link != string::npos)
{//用循环是考虑一行可能有多个链接
string link_name;
string link_value;
string link;
int pos_linkname_left = str.rfind("[",pos_link);
int pos_linkvalue_right = str.find(")",pos_link);
link_name = str.substr(pos_linkname_left+1,pos_link - pos_linkname_left-1);
link_value = str.substr(pos_link+2,pos_linkvalue_right - pos_link - 2);
link = "<a href=\""+link_value+"\">"+link_name+"</a>";
str.replace(pos_linkname_left,pos_linkvalue_right-pos_linkname_left+1,link);
pos_link = str.find("](");
}
}
int main()
{
freopen("data.txt","r",stdin);
string str;
stringstream ss;
bool list=false;//记录无序列表读入情况用来输出<ul>
bool par = false;//用来记录段落
while(getline(cin,str))
{
if(str == "")
{
if(list)
{
cout<<"</ul>"<<endl;
list = false;
}
if(par)
{
cout<<"</p>"<<endl;
par = false;
}
}
else
{
if(str[0]=='#')
{//处理标题
if(list)
{
cout<<"</ul>"<<endl;
list = false;
}
if(par)
{
cout<<"</p>"<<endl;
par = false;
}
ss.str(str);
str = "";//准备用来存放格式化后的标题
string temp;
ss>>temp;
int num_topic=0;
for(int i=0;i<temp.size();i++)
{
if(temp[i] == '#')
num_topic++;
}
temp="";
ss>>str;//之所以先弹出一个,是为了去除#后的空格,下同
getline(ss,temp);
str+=temp;
superlink(str);
check(str);
cout<<"<h"<<num_topic<<">"<<str<<"</h"<<num_topic<<">"<<endl;
}
else if (str[0] == '*')
{//处理无序列表
if(!list)
{
//如果之前没有列表
cout<<"<ul>"<<endl;
list = true;
}
if(par)
{
cout<<"</p>"<<endl;
par = false;
}
ss.str(str);
str = "";
string temp;
ss>>temp;
temp="";
ss>>str;
getline(ss,temp);
str+=temp;
superlink(str);
check(str);
cout<<"<li>"<<str<<"</li>"<<endl;
}
else
{//如果以上两种都不是,则应该是段落
if(list)
{
cout<<"</ul>"<<endl;
list = false;
}
if(!par)
{
//如果之前没有出现段落
cout<<"<p>";
par = true;
}
else
{
cout<<endl;
}
superlink(str);
check(str);
cout<<str;
}
}
ss.clear();
}
//如果列表处于最后一行,读取结束后还需判断是否需要输出</ul>
if(list)
cout<<"</ul>"<<endl;
if(par)
cout<<"</p>"<<endl;
return 0;
}
CCF CSP 201703-3 满分AC
最新推荐文章于 2021-05-25 18:04:09 发布