CCF markdown

原题:http://118.190.20.162/view.page?gpid=T55


很恶心的题目,分数从70变成60变成50变成70,最后终于AC了....发现是find函数的pos忘记初始化成第一次找到的位置了orz....


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
string handle_em(string buf);
string handle_link(string buf);

string handle(string buf)
{
	int post, posl;
	post = buf.find("_",0);
	posl = buf.find("[",0);
	if(post == string::npos && posl != string::npos || post != string::npos && posl != string::npos && posl < post)
		return handle_link(buf);
	else if(post != string::npos && posl == string::npos || post != string::npos && posl != string::npos && post < posl)
		return handle_em(buf);
	else
		return buf;
}

string handle_em(string buf)
{
	int pos1 = 0, pos2;
	while (buf.find("_", pos1) != string::npos)
	{
		pos1 = buf.find("_", pos1);
		pos2 = buf.find("_", pos1+1);
		string tmp = buf.substr(pos1+1, pos2-pos1-1);
		if (tmp.find("[", 0) != string::npos)
			tmp = handle_link(tmp);
		string ans = "<em>";
		ans += tmp;
		ans += "</em>";
		buf.replace(pos1, pos2 - pos1 + 1, ans);
		pos1 += ans.length();
	}
	return buf;
}

string handle_link(string buf)
{
	int pos1 = 0;
	int i;
	while (buf.find("[", pos1) != string::npos)
	{
		pos1 = buf.find("[",pos1);
		string text;
		for (i = pos1 + 1; buf[i] != ']'; i++)
			text += buf[i];
		if (buf[++i] != '(')
			break;
		i++;
		string link;
		for (; buf[i] != ')'; i++)
			link += buf[i];
		string tlink = link, ttext = text;
		if (tlink.find("_", 0) != string::npos)
			tlink = handle_em(tlink);
		if (ttext.find("_", 0) != string::npos)
			ttext = handle_em(ttext);
		string ans = "<a href=\"" + tlink + "\">" + ttext + "</a>";
		buf.replace(pos1, text.length() + link.length() + 4, ans);
		pos1 += ans.length();
	}
	return buf;
}


int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	string line;
	int list_flag = 0;
	int para_flag = 0;
	while (getline(cin, line))
	{
		if (line[0] != '*' && list_flag == 1)
		{
			printf("</ul>\n");
			list_flag = 0;
		}
		if (line[0] == 0)//删除空行
		{
			if (para_flag == 1)
			{
				printf("</p>\n");
				para_flag = 0;
				continue;
			}
			else
				continue;
		}
		else if (line[0] == '#')//标题
		{
			int cnt = 0, i;
			for (i = 0; line[i] == '#'; i++)
				cnt++;
			string head;//标题内容
			for (; line[i] == ' '; i++);
			if (line[i] == 0)
				head = "";
			else
			{
				for (; line[i] != 0; i++)
					head += line[i];
			}
			head = handle(head);
			printf("<h%d>%s</h%d>\n", cnt, head.c_str(), cnt);
		}
		else if (line[0] == '*')//无序列表
		{
			if (list_flag == 0)
			{
				printf("<ul>\n");
				list_flag = 1;
			}
			string buf;
			int i;
			for (i = 1; line[i] == ' '; i++);
			for (; line[i] != 0; i++)
				buf += line[i];
			buf = handle(buf);
			printf("<li>%s</li>\n", buf.c_str());
		}
		else//段落
		{
			if (para_flag == 0)
			{
				printf("<p>");
				para_flag = 1;
			}
			else
				printf("\n");
			line = handle(line);
			printf("%s", line.c_str());
			
		}
	}
	if (para_flag == 1)
		printf("</p>\n");
	if (list_flag == 1)
		printf("</ul>\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值