试题编号: | 202006-3 |
试题名称: | Markdown渲染器 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: |
|
/*把能够连续输出的内容添加到同一个vec项中
样例1中vec列表的内容
类型 字符串
3 CSP
0
3 CSP is a real realrealrealrealreal competition.
0
3 Come and join us
0
样例2中vec列表的内容
类型 字符串
1 CSP
0
1 CSP is * a real competition.
1 * Come! and join.
0
3 *Tel:
0
1 12345
1
根据输出的宽度计算要输出行数,注意!!每行输出的开头不能是空格
注意!!在样例中,段落10个字符(w)为一行,项目列表7个字符(w-3)为一行*/
#include<iostream>
#include<vector>
using namespace std;
typedef struct Markdown{
int type;//标记类型,0 空行,1 项目列表第一行,2 项目列表其余行,3 段落
string s;
}Markdown;
vector<Markdown> vec;
bool isSpace(string s){//判断是否为空行
for(int i=0;i<s.size();i++){
if(s[i]!=' ')return false;
}
return true;
}
string standard(string s){//去除首尾的空格
int pos1 = 0, pos2 = s.length() - 1;
for (int i = 0; i < s.length(); i++){
if (s[i] != ' '){
pos1 = i;
break;
}
}
for (int i = s.length() - 1; i >= 0; i--){
if (s[i] != ' '){
pos2 = i;
break;
}
}
string temp = s.substr(pos1, pos2 - pos1 + 1);
return temp;
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
Markdown temp;
int w,flag=0;//flag标记当前输入行的前一行的类型
cin >> w;
string str;
while(getline(cin,str)){
if(isSpace(str)){//处理空行
if(flag!=0){//上一行不是空行,就添加一行空行
temp.s="";temp.type=0;
vec.push_back(temp);
flag=0;
}
continue;
}
if(str.size()>=2 && str[0]=='*' && str[1]==' '){//处理项目列表第一行
if(flag==3){//上一行是段落,就插入一行空行隔开
temp.s="";temp.type=0;
vec.push_back(temp);
}
//然后在把项目列表放入vec
temp.type=1;temp.s=standard(str.substr(2,str.size()));
vec.push_back(temp);
flag=1;
} else if(str.size()>=2 && str[0]==' ' && str[1]==' ' && (flag==1 || flag==2)){//处理项目其余行
if(vec[vec.size()-1].s==""){//处理特殊情况,即项目列表第一行为空
vec[vec.size()-1].s=standard(str);
}else{
vec[vec.size()-1].s=vec[vec.size()-1].s+" "+standard(str);
}
flag=2;
}else{//处理段落
if(flag==1 || flag==2){
temp.s="";temp.type=0;
vec.push_back(temp);
temp.s=standard(str);
temp.type=3;
vec.push_back(temp);
}else if(flag==3){
vec[vec.size()-1].s=vec[vec.size()-1].s+" "+standard(str);
}else{
temp.s=standard(str);
temp.type=3;
vec.push_back(temp);
}
flag=3;
}
}
long long res = 0;//结果,记录行数
if(vec[vec.size()-1].type==0)res=-1;//若最后一行是空行,就去掉,即令res从-1开始
for(int i=0;i<vec.size();i++){
int type=vec[i].type;
string s=vec[i].s;
if(type==0){//空行结果+1
res++;
//cout<<endl;
}else if(type==1 || type==2){//项目列表
if(s.size()==0){//处理特殊情况,即项目列表为空,行数+1
res++;
continue;
}
int t=0;
while(t<s.size()){
while(1){//保证每行开头不是空格
if(t>=s.size())break;
if(s[t]!=' ')break;
t++;
}
//cout<<s.substr(t,w-3)<<endl;
t+=(w-3);
res++;
}
}else{
int t=0;
while(t<s.size()){
while(1){//保证每行开头不是空格
if(t>=s.size())break;
if(s[t]!=' ')break;
t++;
}
//cout<<s.substr(t,w)<<endl;
t+=w;
res++;
}
}
}cout<<res<<endl;
}
更多相关CCF的试题解答,请点击>>CCF历年认证考试解答