ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1700. Awakening

I have time limit on 10 test here my code how improve to get ac?
Posted by Alexander Goncharov 28 Aug 2012 13:19
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<list>
#include<vector>
#include <set>

using namespace std;

int main()
{
map<string,vector <string> > stored;
int n;
cin >> n;
string str;
for(int i=0;i<n;i++)
{
cin>>str;
str.erase(str.length()-1,str.length());
vector<string> vec;
while(cin.peek()!='\n')
{
string tmp;
cin>>tmp;
vec.push_back(tmp);
}
stored[str]=vec;
}
int m;

cin>>m;
for(int i=0;i<m;i++)
{
cin.ignore();
vector<string> tmp;
while(cin.peek()!='\n' && !cin.eof())
{
string tmp1;
cin>>tmp1;
tmp.push_back(tmp1);
}
vector<string> v1 = stored[tmp[0]];
for (int i = 1; i<tmp.size(); i++) {
vector<string> v2 = stored[tmp[i]];
std::vector<string> v(v1.size()+v2.size());

// Sort as required by set_intersection
std::sort(v1.begin(),v1.end());
std::sort(v2.begin(),v2.end());
// Compute
std::vector<string>::iterator it = std::set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());

// Display
v.erase(it,v.end());
//for(std::vector<string>::iterator it = v.begin();it < v.end(); ++it) std::cout<<*it<<" ";
//cout << endl;
v1=v;
}
if (v1.size()!=0) {
for (int i = 0; i<v1.size(); i++) {
cout << v1[i] << " ";
}
cout << endl;
} else {
cout << "No solution." << endl;
}
}
system("pause");
return 0;
}