萌新求调
查看原帖
萌新求调
646208
Hughpig楼主2025/1/20 19:47

RT,实在调不出了,前前后后交了八十多发了呜呜呜

保证match error和unclosed mark没问题,我估计quote可能有锅,但调不动了

 #include<bits/stdc++.h>
using namespace std;

constexpr int N=1e6+7;

int n;
string text[N];

stack<string> stk;

bool check_match(){
	bool flag=0,is_quote=0;
	string tmp;
	for(int i=1;i<=n;++i){
		for(auto c:text[i]){
			if(c=='['){
				tmp="";
				flag=1;
				continue;
			}
			if(c==']'){
				if(is_quote==1){
					if(tmp=="/quote"){
						is_quote=0;
						stk.pop();
					}
					flag=0;
					tmp="";
					continue;
				}
				if(tmp[0]=='/'){
					if(stk.size()==0)return 0;
					string qwq=stk.top();
					stk.pop();
					qwq="/"+qwq;
					if(tmp=="/url"){
						if(qwq.substr(0,5)!="/url=")return 0;
						else continue;
					}
					if(tmp=="/img"){
						if(qwq.substr(0,5)!="/img=")return 0;
						else continue;
					}
					if(qwq!=tmp)return 0;
				}
				else stk.push(tmp);
				if(tmp=="quote")is_quote=1;
				flag=0;
				tmp="";
			}
			if(flag){
				tmp+=c;
			}
		}
	}
	return 1;
}

string change(string s){
	if(s=="h1")return "# ";
	if(s=="/h1")return " #";
	if(s=="h2")return "## ";
	if(s=="/h2")return " ##";
	if(s=="i"||s=="/i")return "*";
	if(s=="b"||s=="/b")return "__";
	return "QwQ";
}

void work(){
	bool flag=0,is_quote=0,actived=0,transed=0;
	string tmp,cache,_cache;
	for(int i=1;i<=n;++i){
		if(actived&&text[i].size()==0&&is_quote)_cache+="> \n";
		transed=0;
		for(int j=0;j<text[i].size();++j){
			if(is_quote)actived=1;
			char c=text[i][j];
			if(transed==0&&actived){
				if(is_quote&&text[i].substr(0,8)!="[/quote]"){
					transed=1;
					//if(_cache.size())cout<<"\n";
					//cout<<_cache.size()<<' ';
					cout<<_cache<<"> ";
					_cache="";
				}
			}
			if(c=='['){
				if(flag==1)cout<<cache;
				cache="[";
				cout<<tmp;
				tmp="";
				flag=1;
				continue;
			}
			if(c==']'){
				flag=0;
				if(is_quote){
					if(tmp=="/quote"){
						is_quote=0;
						if(j!=text[i].size()-1)cout<<"\n";
					}
					else{
					    cout<<'['<<tmp<<']';
					}
					tmp="";
					continue;
				}
				if(tmp.substr(0,4)=="url="){
					stk.push(tmp.substr(4));
					cout<<'[';
					tmp="";
					continue;
				}
				if(tmp.substr(0,4)=="img="){
					stk.push(tmp.substr(4));
					cout<<"![";
					tmp="";
					continue;
				}
				if(tmp=="/url"||tmp=="/img"){
					cout<<"]("<<stk.top()<<")";
					stk.pop();
					tmp="";
					continue;
				}
				if(tmp=="quote"){
					/*if(j==6&&text[i].size()>7)cout<<"> ";
					else if(j!=text[i].size()-1)cout<<"\n> ";*/
					//if(j==7&&j!=text[i].size()-1)cout<<"> ";
					if(j>6)cout<<'\n';
					actived=0;
					is_quote=1;
					_cache="";
					tmp="";
					continue;
				}
				cout<<change(tmp);
				tmp="";
				continue;
			}
			if(flag){
				tmp+=c;
				cache+=c;
			}
			else cout<<c;
		}
		if(flag)cout<<cache,cache="";
		if((is_quote&&!actived)||(is_quote&&actived&&text[i].size()==0)){
			//cout<<"111";
			continue;
		}
		if(text[i]=="[/quote]"){
			//cout<<"222";
			continue;
		}
		cout<<"\n";
	}
}

int main(){
	//ios::sync_with_stdio(0);
	//cin.tie(0);
	while(getline(cin,text[++n]));
	--n;
	if(!check_match())cout<<"Match Error",exit(0);
	if(stk.size())cout<<"Unclosed Mark",exit(0);
	work();
}
2025/1/20 19:47
加载中...