Nie mieszczę się w limicie czasowym kiedy wykonuję następujące zadanie: http://pl.spoj.com/problems/NAMES/ Nie mam pomysłów co jeszcze zmienić żeby przyspieszyć działanie programu. W zmiennej "t" przechowuję liczbę testów, chyba tak to się robi ? Macie jakieś wskazówki ?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class rekord
{
public:
string name;
int counter;
rekord(string cName, int cCounter)
{
name = cName;
counter = cCounter;
}
};
bool mySort(rekord p1, rekord p2)
{
if(p1.counter > p2.counter)
{
return true;
}
else
{
return false;
}
}
int main()
{
int t = 100000;
vector<string> names;
vector<rekord> records;
string input;
string name;
int counter = 1;
for(int i = 0; i < t; ++i)
{
getline(cin, input);
name = input.substr(input.rfind(" ") + 1);
transform(name.begin(), name.end(), name.begin(), ::toupper);
names.push_back(name);
}
sort(names.begin(), names.end());
for(int i = 0; i < names.size(); ++i)
{
counter = 1;
name = names[i];
if(i == (names.size() - 1))
{
records.push_back(rekord(name, counter));
}
for(int j = i + 1; j < names.size(); ++j)
{
if(name == names[j])
{
++counter;
if(j == (names.size() - 1))
{
i += (counter - 1);
records.push_back(rekord(name, counter));
}
}
else
{
if(counter > 1)
{
i += (counter - 1);
}
records.push_back(rekord(name, counter));
break;
}
}
}
sort(records.begin(), records.end(), mySort);
for(int i = 0; i < records.size(); ++i)
{
cout<<records[i].name<<" "<<records[i].counter<<endl;
}
return 0;
}