【算法——嵌套递归】

力扣772


int where = 0;

void push(vector<int>&number, vector<char>&symbol, int cur, char op)
{
	int n = number.size();
	if (n == 0 || symbol[n - 1] == '+' || symbol[n - 1] == '-')
	{
		number.push_back(cur);
		symbol.push_back(op);
	}
	else
	{
		int top_num = number[n - 1];
		char top_op = symbol[n - 1];
		if (top_op == '*')
			number[n - 1] = top_num * cur;
		else
			number[n - 1] = top_num / cur;
		symbol[n - 1] = op;
	}

}

int operation(vector<int>number, vector<char>symbol)
{
	int n = number.size();
	int ans = number[0];
	for (int i = 1; i < n; i++)
	{
		symbol[i - 1] == '+' ? ans += number[i]  : ans -= number[i];
	}
	return ans;
}


int dfs(string str, int i)
{
	int cur = 0;
	vector<int>number;	
	vector<char>symbol;
	while (i < str.size() && str[i] != ')')
	{
		if (str[i] <= '9' && str[i] >= '0')
		{
			cur = cur * 10 + str[i++] - '0';
		}
		else if (str[i] != '(')
		{
			push(number, symbol, cur, str[i++]);
			cur = 0;
		}
		else
		{
			cur = dfs(str, i + 1);
			i = where + 1;
		}

	}
	push(number, symbol, cur, '+');
	where = i;
	return operation(number,symbol);
}

394. 字符串解码

int where = 0;      //保存当前子过程遇']'时候的索引,然后返回,父过程就可以继续遍历
 
string get(string path, int cur)    //进行运算,3[a]这种
{
	string str2;
	while (cur--)
		str2.append(path);

	return str2;
}

string dfs(string str, int index)
{
	//每一个递归(过程)都会有这俩东西,一个是计算数,一个保存当前遍历
	int cur = 0;    
	string path;
	while (index < str.size() && str[index] != ']')
	{
		if (str[index] >= 'a' && str[index] <= 'z' || str[index] >= 'A' && str[index] <= 'Z')
		{
			path.push_back(str[index++]);
		}
		else if (str[index] <= '9' && str[index] >= '0')
		{
			cur = cur * 10 + str[index++] - '0';
		}
		else if (str[index] == '[')
		{
			path.append(get(dfs(str, index + 1), cur));   //当前path追加
			cur = 0;        //cur经过上面过程已经“变现了”,所以清空,等待下一个数字
			index = where + 1;     //子过程的where指向],所以我们指向]下一个继续遍历
		}
	}

	where = index;  //当遇到]where进行保存,交给父过程

	return path;
}

726. 原子的数量

#include<iostream>
#include<string>
#include<map>
using namespace std;

int where = 0;

void fill(map<string,int>&ans,map<string,int>table,string name,int cur)
{
	if (name.size() > 0 || !table.empty())
	{
		cur = cur == 0 ? 1 : cur;
		if (name.size() > 0)
		{
			auto i = ans.find(name);
			if (i == ans.end())
				ans.emplace(name, cur);
			else
				ans[name] += cur;
		}
		else
		{
			for (auto i : table)
			{
				auto j = ans.find(i.first);
				if (j == ans.end())
					ans.emplace(i.first, i.second * cur);	
				else
					ans[i.first] +=(i.second)*cur;
			}

		}
	}



}

map<string,int> dfs(string str, int index)
{
	string name;
	map<string, int>ans;
	map<string, int>table;
	int cur = 0;
	while (index < str.size() && str[index] != ')')
	{
		if (str[index] >= 'A' && str[index] <= 'Z' || str[index]=='(')
		{
			//历史填写和清空历史
			fill(ans, table, name, cur);
			table.clear();
			name.clear();
			cur = 0;

			if (str[index] >= 'A' && str[index] <= 'Z')
				name.push_back(str[index++]);
			else
			{
				table = dfs(str, index + 1);
				index = where + 1;
			}	
		}
		else if (str[index] <= '9' && str[index] >= '0')
		{
			cur = cur * 10 + str[index++] - '0';
		}
		else if(str[index]>='a'&&str[index]<='z')
		{
			name.push_back(str[index++]);

		}
	}
	fill(ans, table, name, cur);
	where = index;
	return ans;
}

string f(string str)
{
	string ans;
	map<string, int>ma = dfs(str, 0);
	for (auto i : ma)
	{
		ans.append(i.first);
		int num = i.second;
		if (num > 1)
			ans.append(to_string(num));
	}
	return ans;

}

int main()
{
	string str;
	cin >> str;
	cout<<f(str);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值