是string的问题吗?
查看原帖
是string的问题吗?
181805
天马行空mz楼主2023/1/30 11:51
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 200000
stack<char> q;//字符 
stack<int> x;//位置
string s; 
int f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>s;
	q.push('-');
	x.push(-1);
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='('||s[i]=='[')//直接放入 
		{
			q.push(s[i]);x.push(i);
		}	
		else if(s[i]==')'&&q.top()!='(')//未匹配 
		{
			f[i]=1;
		}
		else if(s[i]==']'&&q.top()!='[')//未匹配 
		{
			f[i]=2;
		}
		else if(s[i]==')'&&q.top()=='(')//匹配 
		{
			q.pop();x.pop();
		} 
		else if(s[i]==']'&&q.top()=='[')//匹配 
		{
			q.pop();x.pop();
		}	
	}
	while(!q.empty())
	{
		if(q.top()=='('||q.top()==')')
		{
			f[x.top()]=1;
			x.pop();q.pop();
		}
		else
		{
			f[x.top()]=2;
			x.pop();q.pop();			
		}
		if(q.top()=='-') break;
	}
	for(int i=0;i<s.length();i++)
	{
		if(f[i]==1) cout<<"()";
		else if(f[i]==2) cout<<"[]";
		else cout<<s[i];
	} 
	cout<<endl;
	return 0;
} 

跑代码,跑那种不合适的样例没有问题,但是一旦跑平衡括号序列,例如“([])”,就直接卡死在第一循环这里,拿这个代码跑只有AC和RE。

2023/1/30 11:51
加载中...