题目1 : Popular Products
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given N lists of customer purchase, your task is to find the products that appear in all of the lists.
A purchase list consists of several lines. Each line contains 3 parts: the product id (format XXXX-XXXX), the purchase date (format mm/dd/yyyy) and the price (with decimal places). Two product are considered equal if both the product id and the price are equal.
输入
The first line contains an integer N denoting the number of lists. (1 ≤ N ≤ 1000)
Then follow N blocks. Each block describes one list.
The first line of each block contains an integer M denoting the number of products in the list. (1 ≤ M ≤ 50)
M lines follow. Each line contains the product id, the purchase date and the price.
输出
The products that appear in all of the lists. You should output the product id in increasing order.
If two different product share the same id (with different price) you should output the id twice.
样例输入
3
2
1111-1111 07/23/2016 998.00
1111-2222 07/23/2016 888.00
2
1111-2222 07/23/2016 888.00
1111-1111 07/23/2016 998.00
2
1111-2222 07/23/2016 888.00
1111-1111 07/23/2016 999.00
样例输出
1111-2222
分析:题目意思是找出在每个list中出现过的商品,题目中说商品id相同但是价格不同的商品是算两种不同的商品的。注意到输出只要输出商品id而与price无关,所以直接讲id和price组合起来形成一个字符串直接处理。对于每个list中可能有同样的商品出现多次于是用一个set对这些商品进行去重,然后用map将该商品的id+price的字符串和出现次数存下来。最后遍历整个map找到出现次数为N的元素,这就是要找的答案,然后输出其id。
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
int N,M;
cin>>N;
map<string,int>mp;
string id,date;
string price;
for (int i = 0; i < N; ++i){
cin>>M;
set<string>st;
while(M--){
cin>>id>>date>>price;
st.insert(id+price);
}
set<string>::iterator it = st.begin();
for (it = st.begin(); it != st.end(); ++it){
mp[*it]++;
}
}
map<string,int>::iterator it ;
for (it = mp.begin(); it != mp.end(); ++it){
if((*it).second == N){
cout<<(*it).first.substr(0,9)<<endl;
}
}
return 0;
}