社区讨论

P7914[CSP-S 2021] 括号序列参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@ltc9q57y
此快照首次捕获于
2024/03/04 09:34
2 年前
此快照最后确认于
2024/03/04 16:10
2 年前
查看原帖
输出一直是1
CPP
#include<bits/stdc++.h>
using namespace std;
int n,k;
string s;
int ans;
int a[510];
void check()
{
	int zk=0,ch=0;
	for(int i=0;i<n;++i)
	{
		if(s[i]=='(')
		{
			zk++;
			ch=0;
		}
		else if(s[i]==')')
		{
			if(!zk)
			{
				return;
			}
			zk--;
			ch=0;
		}
		else if(s[i]=='*')
		{
			ch++;
			if(ch>k)
			{
				return;
			}
		}
	}
	if(s[n-1]!=')')
	{
		return;
	}
	ans++;
	return;
}
void dg(int x)
{
	if(x==n)
	{
		check();
		return;
	}
	if(s[x]=='?')
	{
		s[x]='(';
		dg(x+1);
		s[x]=')';
		dg(x+1);
		s[x]='*';
		dg(x+1);
		return;
	}
	dg(x+1);
	return;
}
int main()
{
	cin>>n>>k;
	cin>>s;
	dg(0);
	cout<<ans;
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...