This solution gets "Wrong answer" result inhttp://www.programming-challenges.com/,
but it gets "Accepted" result inhttp://uva.onlinejudge.org/.
According to the explanation in http://www.programming-challenges.com/problems_specific.txt, I think my program should be correct, and it is the problem of http://www.programming-challenges.com/to judge my program as "Wrong answer".
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct Tortoise
{
string m_name;
int m_index;
};
typedef Tortoise* TortoisePtr;
static void ShellSort(TortoisePtr* tortoises, int cnt)
{
if (cnt <= 1)
return;
int toHandleMaxTortoiseIndex = 0;
while(tortoises[toHandleMaxTortoiseIndex]->m_index != (cnt - 1))
++toHandleMaxTortoiseIndex;
int searchSecondMaxFrom = (toHandleMaxTortoiseIndex == 0) ? 1 : 0;
for (int k = cnt; k >= 2; --k)
{
int j = searchSecondMaxFrom;
while(tortoises[j]->m_index != (k - 2))
++j;
if (toHandleMaxTortoiseIndex < j)
{
cout << tortoises[j]->m_name << endl;
TortoisePtr temp = tortoises[j];
for (int x = j; x >= 1; --x)
tortoises[x] = tortoises[x - 1];
tortoises[0] = temp;
toHandleMaxTortoiseIndex = 0;
++searchSecondMaxFrom;
}
else
toHandleMaxTortoiseIndex = j;
}
}
static void RunTestCase()
{
int cnt;
cin >> cnt;
cin.ignore(); // Consume the '\n' after the count of tortoises.
TortoisePtr* tortoises = new TortoisePtr[cnt];
for (int i = 0; i < cnt; ++i)
{
tortoises[i] = new Tortoise();
getline(cin, tortoises[i]->m_name);
}
map<string, int> queueInfo;
string temp;
for (int i = 0; i < cnt; ++i)
{
getline(cin, temp);
queueInfo.insert(pair<string, int>(temp, i));
}
for (int i = 0; i < cnt; ++i)
tortoises[i]->m_index = queueInfo[tortoises[i]->m_name];
ShellSort(tortoises, cnt);
for (int i = 0; i < cnt; ++i)
delete tortoises[i];
delete[] tortoises;
}
static void Test()
{
int cnt;
cin >> cnt;
for (int i = 0; i < cnt; ++i)
{
RunTestCase();
cout << endl;
}
}
int main(int argc, char* argv[])
{
Test();
return 0;
}