2013amazon网上测试题

问题1:

As you know, two operations of Stack are push and pop. Now give you two integer arrays, one is the original array before push and pop operations, the other one is the result array after a series of push and pop operations to the first array. Please give the push and pop operation sequence.


For example:
If the original array is a[] = {1,2,3}, and the result array is b[] = {1,3,2}.
Then, the operation sequence is “push1|pop1|push2|push3|pop3|pop2”(operations are split by ‘|’ and no space).


Rules:
The push and pop operations deal with the original int array from left to right.
The input is two integer array. They are the original array and the result array. These interger array is split by space.
The output is the operation sequence.
If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
The operation "push1" means push the first element of the original array to the stack.
The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail of the result array.
Please don't include any space in the output string.
Sample1: 
Input:
1 2 3 4
1 2 3 4
Output:
push1|pop1|push2|pop2|push3|pop3|push4|pop4

Sample2: 
Input:
1 2 3 4
4 3 2 1
Output:

push1|push2|push3|push4|pop4|pop3|pop2|pop1

 

解答:

#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <stdio.h>

using namespace std;

stack<int> sta;//栈记录
vector<string> seq;//操作序列
vector<int> popSeq;//出栈序列
bool flag = false;

//arr:入栈序列  out:出栈序列  sta:当前栈中数据  current:当前处理到的arr中的位置 len:入栈序列长度
bool g(int arr[], int out[], int current, int len)
{
    //如果已经找到操作序列,其它的即不用再操作
    if (flag)
        return true;

	//操作1:入栈
	if (current < len)
	{
		char str1[10];
		sprintf(str1, "push%d", arr[current]); //产生push1
		seq.push_back(str1);//记录序列加1
		sta.push(arr[current]);//当前值入栈
		g(arr, out, ++current, len);//处理下一个值
		int temp1 = sta.top();
		current--;//插入元素的位置也要恢复
		sta.pop();//恢复之前的状态
		seq.pop_back();//记录序列减1
	}
	//否则,最后一个已经入栈

	//操作2:出栈
	if (!sta.empty())
	{
		int temp2 = sta.top();
		char str2[10];
		sprintf(str2, "pop%d", temp2); //产生push1
		seq.push_back(str2);//记录出栈序列
		popSeq.push_back(temp2);
		sta.pop();
		g(arr, out, current, len);//处理的还是当前值,只不过出栈一个元素
		sta.push(temp2);//恢复之前的状态
		seq.pop_back();//恢复出栈序列
		popSeq.pop_back();
	}

	//结束判断
	if (seq.size() == len * 2)
	{
	    for (int i = 0; i < len; i++)
        {
            if (popSeq[i] != out[i])
            {
                return false;//add
            }
        }

        for (int tt = 0; tt < len*2 - 1; tt++)
		{
		    cout<<seq[tt]<<"|";
		}
		cout<<seq[len*2 - 1]<<endl;
		flag = true;
        return true;//add


		//这里可输出所有序列
		/*for (int tt = 0; tt < len*2; tt++)
		{
		    cout<<seq[tt];
		}
		cout<<endl;

		for (int tt = 0; tt < len; tt++)
		{
		    cout<<popSeq[tt];
		}
		cout<<endl;*/
	}

	return false;//add

}

int main()
{
	int arr[] = {1,2,3};
	int out[] = {1,3,2};
	int len = 3;
	g(arr, out, 0, len);
	if (!flag)
        cout<<"None"<<endl;

	int tt;
	cin>>tt;

	return 0;
}


问题2:

We have an array representing customer’s shopping records.
For example, it’s an array like this:

custA, item1,
custB, item1,
custA, item2,
 custB, item3,
 custC, item1,
 custC, item3,
 custD, item2,

This array indicates that customer A bought item 1, customer B bought item 1, customer A bought item 2, customer B bought item 3, etc..
For a given item X and shopping records array, write code to find out what else (item Y) was bought mostly by the customers who bought item X.
For example, in above example, if X is item 1 then Y should be item 3.

Rules:
One customer can only buy one item once.
The mostly brought item should not be item X.
If no customer brought item X, then return “None”
If all the customers who brought item X only brought item X, then return “None”
The first line of input is the item X. The second line of input is the shopping record array, this shopping record array is split by space.
If there are many other mostly brought items which have equally brought times, then return any one of those items.
Examples:

Input1:
item1
custA item1 custB item1 custA item2 custB item3 custC item1 custC item3 custD item2

Output1:
item3

Input2:

item2
custA item1 custB item1 custC item1 custA item2 custB item3 custA item3

Output2:
item1  
(The output2 can be item3 too)

 

解答:

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
	//测试1开始
	/*multimap<string, string> customer2orders;
	customer2orders.insert(make_pair("custA","item1"));
	customer2orders.insert(make_pair("custB","item1"));
	customer2orders.insert(make_pair("custC","item1"));
	customer2orders.insert(make_pair("custA","item2"));
	customer2orders.insert(make_pair("custB","item3"));
	customer2orders.insert(make_pair("custA","item3"));

	multimap<string, string> orders2customer;
	orders2customer.insert(make_pair("item1","custA"));
	orders2customer.insert(make_pair("item1","custB"));
	orders2customer.insert(make_pair("item1","custC"));
	orders2customer.insert(make_pair("item2","custA"));
	orders2customer.insert(make_pair("item3","custB"));
	orders2customer.insert(make_pair("item3","custA"));

	map<string, int> itemCount;
	itemCount.insert(make_pair("item1", 0));
	itemCount.insert(make_pair("item2", 0));
	itemCount.insert(make_pair("item3", 0));

	//以该item作为开始
	string inputItem = "item2";*/
	//测试1结束

	//============================================

	//测试2开始
	multimap<string, string> customer2orders;
	customer2orders.insert(make_pair("custA","item1"));
	customer2orders.insert(make_pair("custB","item1"));
	customer2orders.insert(make_pair("custA","item2"));
	customer2orders.insert(make_pair("custB","item3"));
	customer2orders.insert(make_pair("custC","item1"));
	customer2orders.insert(make_pair("custC","item3"));
	customer2orders.insert(make_pair("custD","item2"));

	multimap<string, string> orders2customer;
	orders2customer.insert(make_pair("item1","custA"));
	orders2customer.insert(make_pair("item1","custB"));
	orders2customer.insert(make_pair("item2","custA"));
	orders2customer.insert(make_pair("item3","custB"));
	orders2customer.insert(make_pair("item1","custC"));
	orders2customer.insert(make_pair("item3","custC"));
	orders2customer.insert(make_pair("item2","custD"));

	map<string, int> itemCount;
	itemCount.insert(make_pair("item1", 0));
	itemCount.insert(make_pair("item2", 0));
	itemCount.insert(make_pair("item3", 0));
	itemCount.insert(make_pair("item4", 0));

	string inputItem = "item1";//以该item作为开始
	//测试2结束

	multimap<string,string>::iterator iter;
	pair< multimap<string,string>::iterator, multimap<string,string>::iterator>pos;
	pos=orders2customer.equal_range(inputItem);

	for(iter=pos.first;iter!=pos.second;iter++)
	{
		multimap<string,string>::iterator iter2;
		pair<multimap<string,string>::iterator, multimap<string,string>::iterator>pos2;
		pos2 = customer2orders.equal_range(iter->second);
		for(iter2 = pos2.first; iter2!=pos2.second; iter2++)
		{
			map<string, int>::iterator iter3 = itemCount.begin();
			for (iter3; iter3 != itemCount.end(); iter3++)
			{
				if (iter2->second == iter3->first && iter2->second != inputItem)//除去自身的
				{
					iter3->second += 1;
				}
			}
		}
	}

	string output;
	int max = -1;
	for(map<string,int>::iterator iter4 = itemCount.begin(); iter4 != itemCount.end(); iter4++)
	{
		if (iter4->second > max)
		{
			output = iter4->first;
			max = iter4->second;
		}
	}

	//找出最大的item
	cout<<output<<endl;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值