CCF markdown

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


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


#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 = "";
		ans += tmp;
		ans += "";
		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 = "" + ttext + "";
		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("\n");
			list_flag = 0;
		}
		if (line[0] == 0)//删除空行
		{
			if (para_flag == 1)
			{
				printf("

\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("%s\n", cnt, head.c_str(), cnt); } else if (line[0] == '*')//无序列表 { if (list_flag == 0) { printf("
    \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("
  • %s
  • \n", buf.c_str()); } else//段落 { if (para_flag == 0) { printf("

    "); para_flag = 1; } else printf("\n"); line = handle(line); printf("%s", line.c_str()); } } if (para_flag == 1) printf("

    \n"); if (list_flag == 1) printf("
\n"); return 0; }


你可能感兴趣的:(算法题)