Mógłby mi ktoś wskazać stronę, w którą pójść, aby przyspieszyć algorytm?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int przebiegi;
cin >> przebiegi;
for (int i = 0; i < przebiegi; i++)
{
vector <string> tablica;
int slowa;
cin >> slowa;
for (int x = 0; x < slowa; x++)
{
string slowo, tmp;
cin >> slowo;
tmp+=slowo[0];
tmp+=slowo[slowo.length()-1];
tablica.push_back(tmp);
}
for (int s = 0; s < tablica.size(); s++)
for (int x = 0; x < tablica.size()-1; x++)
if (tablica[x][1] != tablica[x+1][0])
swap(tablica[x], tablica[x+1]);
bool tmp = true;
for (int x = 0; x < tablica.size()-1; x++)
if (tablica[x][1] != tablica[x+1][0])
{
tmp = false;
break;
}
tmp ? cout << "Ordering is possible." << endl : cout << "The door cannot be opened." <<endl;
}
return 0;
}
Przykładowe wejście:
1
2
mo
om
Wyjście
Ordering is possible.