关于读入方式以及内存空间
查看原帖
关于读入方式以及内存空间
518500
Ted_LightningTechG_楼主2023/8/8 10:39

使用这种读入方式,MLE on #9,125MB

#include<bits/stdc++.h>
using namespace std;
string f() {
	char x = getchar();
	int k;
	string s = "", rip = "";
	while( x != '\n' ) {
		if( x == '[' ) {
			cin>> k;
			rip = f();
			while( k --> 0 ) 
				s += rip;
		}
		else if( x == ']' ) return s;
		else s += x;
		x = getchar();
	}
}
int main() {
	cout<< f();
	return 0;
}

然鹅,换成了这种,#9的内存空间变成了684KB?

#include<bits/stdc++.h>
using namespace std;
string f() {
	char x;
	int k;
	string s = "", rip = "";
	while( cin>> x ) {
		if( x == '[' ) {
			cin>> k;
			rip = f();
			while( k --> 0 ) 
				s += rip;
		}
		else if( x == ']' ) return s;
		else s += x;
	}
}
int main() {
	cout<< f();
	return 0;
}

萌新不知道为什么,求大佬解答

2023/8/8 10:39
加载中...