CCF CSP202006-2Markdown渲染器
stl操作
分段落项目空行分别处理,有注释,附上测试用例,两种情况组合答案是25
#include<string>
#include<iostream>
#include<vector>
using namespace std;
//一行文本最大数
long long int maxtext;
//存储文本
vector<string>text;
//去除首尾空格
void delSpace(string &a){
int i = 0;
while(i<a.size()&&a[i]==' '){
a.erase(i,1);
}
while(a.size()>0&&a[a.size()-1]==' '){
a.erase(a.size()-1,1);
}
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>maxtext;
string str,temp="",ans;
long long int count = 0;
cin.get();
while(getline(cin,str)!=NULL){
text.push_back(str);
}
//跳过最开始的空行
int i = 0;
while(text[i]==""&&i<text.size()){
i++;
}
//一个段落或者项目进行处理
for(;i < text.size(); i++){//cout<<count<<endl;
//项目,空行,段落
if(i<text.size()&&text[i].size()>=2&&text[i][0]=='*'&&text[i][1]==' '){
ans = "";
temp = text[i].substr(2);
delSpace(temp);
ans+=temp;
i++;
while(i < text.size()&&text[i].size()>=2&&text[i][0]==' '&&text[i][1]==' '){
temp = text[i];
delSpace(temp);
if(temp.size()==0) {
break;//空白行
}
ans+=' '+temp;
i++;
}
i--;
//渲染文本
temp="";
if(ans.size() == 0) count++;
for(int j = 0,k=0; j < ans.size(); j++,k++){
if(k%(maxtext-3)==0){
count++;
if(k!=0)temp += '\n';
while(ans[j]==' '){
j++;
}
}
temp+=ans[j];
}
if(i+1<text.size()&&text[i+1].size()>=2&&text[i+1][0]=='*'&&text[i+1][1]==' ') continue;
}
else if(text[i].size()==0){
continue;
}
else{
ans = "";
while(i<text.size()){
if(text[i].size() >=2&&text[i][0]=='*'&&text[i][1]==' '){
i--;
break;
}
temp = text[i];
delSpace(temp);
if(temp.size()==0) {
break;//空白行
}
ans+=' '+temp;
i++;
}
if(ans.size() == 0) continue;
//渲染文本
temp="";
for(int j = 0,k=0; j < ans.size(); j++,k++){
if(k%maxtext==0){
count++;
if(k!=0)temp += '\n';
while(ans[j]==' '){
j++;
}
}
temp+=ans[j];
}
}
//段落,项目之间空行,最后减1
count++;
}
cout<<(count-1)<<endl;
return 0;
}
//csp
//
//csp is
//a real realrealrealrealreal
// competition.
//
//
//cone and join us
//
//CSP
//
//* CSP is
// * a real
// competition.
//*
// * Come! and join.
//*Tel:
//* 12345
//*