CCF CSP 201703-3 满分AC

# 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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值