目录
参考了CCF CSP202006-3 Markdown渲染器的代码,将其中一些判断改为了find。
题目
见 202006-3
c++代码如下,在devc++上运行通过,也可以可以在vs上运行。
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
using namespace std;
//规定 1是项目,2是子项目,3是段落
vector<pair<string, int> > vec;
int w;
void trim(string& str) {
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
}
int main() {
// 用于加速,不知道是不是这个导致getchar有问题?
ios::sync_with_stdio(false);
cin >> w;
string temp;
bool flag = true;
getchar();//问题出在这里
while (getline(cin, temp)) {
/*cout << "输入的temp是" << temp<<endl;*/
if (temp.find_first_not_of(" ") == -1) {
//上一行是空行
flag = true;
continue;
}
if (flag) {
if (temp.find("* ") == 0) {
string mid = temp.substr(2);
trim(mid);
vec.push_back(pair<string, int>(mid, 1));
}
else {
trim(temp);
vec.push_back(pair<string, int>(temp, 3));
}
}
else {
pair<string, int>& last = vec.back();
//cout <<"debug:"<< last.first << endl;
if (last.second <= 2) {
if (temp.find("* ") == 0) {
string mid = temp.substr(2);
trim(mid);
vec.push_back(pair<string, int>(mid, 2));
}
else if (temp.find(" ") == 0) {
string mid = temp.substr(2);
trim(mid);
if (last.first.length() != 0)
last.first += " ";
last.first += mid;
}
else {
trim(temp);
vec.push_back(pair<string, int>(temp, 3));
}
}
else {
if (temp.find("* ") == 0) {
string mid = temp.substr(2);
trim(mid);
vec.push_back(pair<string, int>(mid, 1));
}
else {
trim(temp);
last.first += " ";
last.first += temp;
}
}
}
flag = false;
}
flag = true;
int lastnum = vec.begin()->second;
int res = 0;
for (vector<pair<string, int> >::iterator it = vec.begin(); it != vec.end(); it++) {
if (!flag) {
if (it->second != 2) {
res++;
cout << endl;
}
}
flag = false;
lastnum = it->second;
int temp = 0;
if (it->second <= 2) {
temp = 3;
}
int len = it->first.length();
if (len==0) {
res++;
}
for (int i = 0; i < len;) {
if (len - i >= w - temp) {
cout << it->first.substr(i, w-temp) << endl;
i += w - temp;
res++;
while (i<it->first.length()&&it->first[i] == ' ') {
i++;
}
}
else {
cout << it->first.substr(i) << endl ;
res++;
break;
}
}
}
cout << res;
return 0;
}
// 额外的一个测试用例 结果17
/*
10
asdfasdfsdfasfasfasf s sdf sfasda
asdf
134
***************sadf
***
* sadfas
agfs
*
*/
在本地运行我能想到的测试用例都没有问题,可是一提交就发现只有20分。
经过了一个下午的逐句替换为上述博客代码,发现把getchar()去掉就是100 分了。此getchar()的目的是去掉cin读取剩下的一个回车,但是此题其实不去掉也可以;至于为什么会只有20分,目前我不是很懂,希望有大佬可以帮我解除困惑……
最后吐槽一下垃圾ccf,连哪里错了都不讲,害我浪费了好长时间。