华为机试题

华为机试题

试题简介

linux中的makefile中的目录结构一般是这样:

aaa=home
bbb=${aaa}/zhmf
ccc=${bbb}/user/lib

试题要求在输入n行类似上述字符串后输出最后一行将${xxx}替换后的完整字符串。例如:

输入:
	4
	aaa=home
	bbb=${aaa}/zhmf/${ccc}
	ccc=music
	ddd=${bbb}/DJ/Join
输出:
	home/zhmf/music/DJ/Join

代码如下:

#include<iostream>
#include<vector>
#include<set>
#include<sstream>
#include<map>

using namespace std;
map<string, string> m_kv;
void mySplit(const string& src, vector<string>& v, const char mark = ',')
{
	string line;
	istringstream strm(src);
	while (getline(strm, line, mark))
	{
		v.push_back(line);
	}

}
string makeStr(const vector<string>& v)
{
	int max = v.size();
	if (v.size() > 1) {
		string ret;
		for (int i = 0; i < v.size(); i++)
		{
			ret += v[i];
			if(i!=max-1)
				ret += "/";
		}
		return ret;
	}
	return v[0];
}
string getKey(const string& str)
{
	if (str.find("${") != string::npos)
	{
		//获取值
		auto key = str.substr(2, str.find('}') - 2);
		auto value = m_kv[key];
		//如果value又是key?
		// aaaa=hello/${wd}
		//  aaa=bbb/${aaaa}/ccc/${tt}
		vector<string> v_tmp;
		mySplit(value, v_tmp, '/');
		string retstr;
		vector<string> v_str;
		for (int i = 0; i < v_tmp.size(); i++)
		{
			v_str.push_back(getKey(v_tmp[i]));
			//retstr += getKey(v_tmp[i]);
			//if (i > 0)
			//	retstr += "/";
			
		}
		return makeStr(v_str);
		//	return getKey(v_tmp[i]);
	}
	return str;
}
void func()
{
	//  aaa=bbb/${aaaa}/ccc/${tt}
	vector<string> v_s;
	vector<string> v_k;
	int num;
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		string tmpstr;
		cin >> tmpstr;
		v_s.push_back(tmpstr);


	}
	/*int num = 4;
	v_s.push_back("aaa=hello");
	v_s.push_back("bbb=${aaa}/world/${ccc}");
	v_s.push_back("ccc=yes");
	v_s.push_back("ddd=${aaa}/the/${ccc}/${bbb}/over");*/


	//map<string, string> m_kv;
	for (int i = 0; i < v_s.size(); i++)
	{
		string item = v_s[i];
		size_t indx = item.find('=', 0);
		if (indx != string::npos) {
			m_kv[item.substr(0, indx)] = item.substr(indx + 1);
			v_k.push_back( item.substr(0, indx));
		}
			

	}
	//m_kv.at()
	vector<string> v_last;//bbb/${aaaa}/ccc/${tt}
	mySplit(m_kv[v_k[num - 1]], v_last, '/');
	for (int i = 0; i < v_last.size(); i++)
	{
		v_last[i]=getKey(v_last[i]);
	}
	std::cout << "over  " <<makeStr(v_last)<< std::endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值