文章

35

粉丝

0

获赞

144

访问

7.3k

头像
入栈索引,if判断,思路较简单
P1296 北京大学机试题
发布于2025年3月16日 15:36
阅读数 225

#include <bits/stdc++.h>
using namespace std;
void isValid(string s){
	stack<char> st;

	vector<char> v(s.length(),' ');
	for(int i=0;i<s.length();i++){
		char c=s[i];
		if(c=='('){
			st.push(i);//入栈索引 
		}
		else if(c==')'){
			if(st.empty()){
				v[i]='?';
			}else{
				st.pop();
			}
		}
	}
	while(!st.empty()){
		int index=st.top();
		v[index]='$';
		st.pop();
	}
		cout<<s<<endl;
		for(char c: v){
			cout<<c;
		}
		cout<<endl;
	
	 
}
int main() {
   string s;
   while(getline(cin,s)){
   	isValid(s);
   }
   return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发