4pts 递归写法 求调
  • 板块P11186 三目运算
  • 楼主skyx
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/20 21:06
  • 上次更新2024/10/20 22:39:24
查看原帖
4pts 递归写法 求调
1228886
skyx楼主2024/10/20 21:06

记录,ac on #1,MLE and WA

#include <bits/stdc++.h>
using namespace std;
#define ll long long
stack<int> w;
int peidui(string s)//“?” “:”配对
{
	while(!w.empty())w.pop();
	
	int ans=-1;
	for(int j=0;j<s.size();j++)
	{
		if(s[j]=='?') w.push(j);
		if(s[j]==':')
		{
			ans=j;
			
			w.pop();
		}
		if(w.empty()) break;
	}
	return ans;
	
}
int solve(string s,int x)
{
	bool dayu=0;
	int bijiao=-1;
	int wenhao=-1;
	int i=0;
	for(i=0;i<(int)s.size();i++)
	{
		if(s[i]=='>') 
		{
			dayu=1;	
			bijiao=i;
		}
		if(s[i]=='<')
		{
			bijiao=i;
		}
		if(s[i]=='?')
		{
			break;
		}

	}
	int maohao=peidui(s);
	
	int dtj=0;
	for(int j=bijiao+1;j<i;j++)
	{
		dtj*=10;
		dtj+=s[j]-'0';
	}
	if(bijiao!=-1) //有大于或小于
	{
		if((dayu==1 && x>dtj )|| (dayu==0 && x<dtj)) //条件判断
		{
			return  solve(s.substr(wenhao+1,maohao-1),x);
		} 
		else  return solve(s.substr(maohao+1,(int)(s.size()-1)),x);
	}
	else return dtj;
}
int main()
{
	int m,n;
	cin>>m>>n;

	string s;
	cin>>s;
	for(int hh=1;hh<=n;hh++)
	{
		int x;
		cin>>x;
		cout<<solve(s,x)<<endl;
	}
	
	return 0;
}

tks()

2024/10/20 21:06
加载中...