Question:
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)
//your code is here
char* findMostlyBroughtItem(char* shippingRecordArray[], int length, char* givenItem)
{
map<string,set<string> > cust2ItemMap;
map<string,set<string> > item2CustMap;
typedef map<string,set<string> >::iterator itor;
string tempCust,tempItem;
for (int i=0; i<length; i+=2)
{
tempCust=shippingRecordArray[i];
tempItem=shippingRecordArray[i+1];
itor it=cust2ItemMap.find(tempCust);
if(it!=cust2ItemMap.end())
it->second.insert(tempItem);
else
{
set<string> tempSet;
tempSet.insert(tempItem);
cust2ItemMap.insert(make_pair(tempCust,tempSet));
}
it=item2CustMap.find(tempItem);
if(it!=item2CustMap.end())
it->second.insert(tempCust);
else
{
set<string> tempSet;
tempSet.insert(tempCust);
item2CustMap.insert(make_pair(tempItem,tempSet));
}
}
map<string,int> countMap;
string targetItem = givenItem;
set<string>& tempCustSet = item2CustMap.find(targetItem)->second;
for (set<string>::iterator it=tempCustSet.begin();it!=tempCustSet.end();it++)
{
string tempCust = *it;
set<string> &tempItemSet = cust2ItemMap.find(tempCust)->second;
for(set<string>::iterator subIt=tempItemSet.begin();subIt!=tempItemSet.end();subIt++)
{
map<string,int>::iterator ansIt;
ansIt=countMap.find(*subIt);
if(ansIt==countMap.end())
countMap.insert(make_pair(*subIt,1));
else
ansIt->second++;
}
}
string ansItem;
int maxCount =-1;
for (map<string,int>::iterator it=countMap.begin(); it!=countMap.end();it++)
{
if(it->second>maxCount && it->first!=targetItem)
{
ansItem=it->first;
maxCount=it->second;
}
}
if(maxCount>0)
cout<<ansItem<<endl;
else
cout<<"None"<<endl;
return NULL;
}
9/10 testcases passed
TestCase #0
Status: Passed
Your output:
item2
TestCase #1 (Hidden) Status: Failed (Unexpected Error)
Status: Failed (Unexpected Error) segmentation fault(core dumped)
TestCase #2 (Hidden)
Status: Passed
TestCase #3 (Hidden)
Status: Passed
TestCase #4 (Hidden)
Status: Passed
TestCase #5 (Hidden)
Status: Passed
TestCase #6 (Hidden)
Status: Passed
TestCase #7 (Hidden)
Status: Passed
TestCase #8 (Hidden)
Status: Passed
TestCase #9 (Hidden)
Status: Passed
总结:以上为比较一般的思路,用空间换时间,两方面信息都重新组织了,内存消耗相对较大,可以考虑把空间消耗省掉,方案是将购买记录存在一个二维表里(m行n列,行数为customer数目,n为item数目),存在购买关系则置true,这样空间是最省的,通过custom看所购买的item直接看相应行,通过item看custom看相应列;唯一的问题是如何定位序号了,比较简单的是使用两个map<string,int>保存customName(或itemName)到序号的映射;每次操作之前查序号再访问就好;
//your code is here
char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
{
stack<int> iStack;
int posOrigin=0;
int posTarget=0;
iStack.push(originalArray[posOrigin++]);
string ans;
cout<<"push"<<originalArray[0];
while (posTarget<length)
{
if(iStack.size()!=0 && iStack.top()==resultArray[posTarget])
{
cout<<"|pop"<<iStack.top();
iStack.pop();
posTarget++;
}
else
{
cout<<"|push"<<originalArray[posOrigin];
iStack.push(originalArray[posOrigin++]);
}
}
cout<<endl;
return NULL;
}
总结:第二题弱爆了的题目竟然没来得及提交,(题目给的划分字符函数有问题的,以逗号划分而不是空格,坑爹呀),使用itoa居然不支持,加上cstdlib也没有include错误,tc真诡异,还是有点脑残了,直接输出就好了,没必要维持原来的接口,唉~