CCF CSP202006-2Markdown渲染器(c++100)

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
//* 
      



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值