Secondo voi è possibile risolvere crossover utilizzando il template string? Quindi andando a lavorare sulle stringhe tramite le varie funzioni come swap,erase etc?
Oppure è meglio gestire i vari indici dei due array?
Usando le string il programma diventa molto più lento (ogni volta deve copiare la stringa su cui sta lavorando), mentre usando gli indici basta tenere una copia di ogni stringa. Con le string molto probabilmente andrebbe fuori tempo limite.
Qui c’è un esempio di implementazione con le stringhe che, infatti, totalizza 60 punti
Dario
1 Mi Piace
Qui sopra trovi la risposta al tuo problema.
Se può interessarti, l’ho risolto usando soltanto string ed ho fatto 100/100.
#include <fstream>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXL 100008
string crossa(string left, string right, string lsolved, string rsolved) {
while (true) {
//cout << lsolved << " " << left << " " << right << " " << rsolved << endl;
// Prima regola
if (left.empty() || right.empty()) {
lsolved.append(left+right);
reverse(rsolved.begin(), rsolved.end());
return lsolved+rsolved;
}
// Seconda regola
if (left[0] == right[right.length()-1]) {
left.erase(0, 1);
right.erase(right.length()-1, 1);
swap(left, right);
}
// Terza regola
else {
rsolved += left[0];
lsolved += right[right.length()-1];
left.erase(0, 1);
right.erase(right.length()-1, 1);
}
}
}
int main() {
ifstream cin("input.txt");
ofstream out("output.txt");
string s, t;
cin >> s >> t;
string r = crossa(s, t, "", "");
out << r;
out.close();
return 0;
}
1 Mi Piace
Lo immaginavo che qualcuno li avesse usate! Grazie, utilissimo,.
Non riesco a capire a che ti serve reverse nella prima regola[RISOLTO]