【CCF CSP】201703

201703

  1. 最后一个小朋友只要分到的重量>0,则被认为被分到蛋糕
  2. 数组模拟一下
  3. (1)知识小课堂:C++中string的用法
    (2)转换的技巧:string(‘0’)会报错,可转换为string(1, ‘0’),具体原因见上方链接。
    (3)本题的小bug:测试数据里好像 */# 后只有一个空格?
    (4)分段读入,解析文本
#include <bits/stdc++.h>
#define I(a) scanf("%d", &a)
#define O(a) printf("%d", a)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

string line, text;

int main()
{
//  freopen("aaa.txt","r",stdin);
    getline(cin,line); bool f=false;
    while(true)   //getline不读取 换行符 
    {
        if(line.size()) text+=line+"\n";
        else if(line.size()==0 && text.size())
        {
            size_t pos = text.find("_");
            while(pos!=string::npos)
            {
                text.replace(pos, 1, "<em>");
                pos = text.find("_");
                text.replace(pos, 1, "</em>");
                pos = text.find("_");
            }

            pos = text.find("[");
            while(pos!=string::npos)
            {
                size_t nxtp = text.find("]");
                size_t left = text.find("(");
                size_t right = text.find(")");
                string tex = text.substr(pos+1, nxtp-pos-1);
                string link = text.substr(left+1, right-left-1);
                text.replace(pos, right-pos+1, "<a href=\""+link+"\">"+tex+"</a>");
                pos = text.find("[");
            }

            if(text[0]=='#')
            {
                pos = text.find("#");
                while(pos!=string::npos)
                {
                    int i=pos, cnt=0;
                    while(text[i]=='#') 
                    {
                        cnt++;i++;
                    }
                    while(text[i]==' ') i++;
                    text.replace(pos, i-pos, "<h"+string(1, '0'+cnt)+">");
                    pos = text.find("\n");
                    text.insert(pos, "</h"+string(1, '0'+cnt)+">");
                    pos = text.find("#");
                }
            }

            else if(text[0]=='*')
            {
                text.insert(0, "<ul>\n"); text.insert(text.size()-1, "\n</ul>");
                size_t pos=text.find("*");
                while(pos!=string::npos)
                {
                    int i=pos+1;
                    while(text[i]==' ') i++;
                    text.replace(pos, i-pos, "<li>");
                    size_t nxtp = text.find("\n", pos);
                    text.insert(nxtp, "</li>");
                    pos = text.find("*");
                }   
            }

            else{
                text="<p>"+text;
                text.insert(text.size()-1,"</p>");
            }
            cout<<text;
            text="";
        }
        line="";
        if(f) break;
        if(!getline(cin, line)) f=true;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34898866/article/details/79595931