~cpp // no10152 - Shell Sort #include <iostream> #include <cstring> using namespace std; const int MAX_NAME = 81; const int MAX_TURTLE = 200; void input_turtle(char turt[][MAX_NAME], const int & nTurt); void move_turtle(const int * code, const int & nTurt, bool * isMove); void incoding(const char oldTurt[][MAX_NAME], const char newTurt[][MAX_NAME], const int & nTurt, int * code); void print_turtle(const char turt[][MAX_NAME], const int * code, const bool * isMove, const int & nTurt); inline void show_turtle(const char turt[]) { cout << turt << endl; }; int main() { char oldTurt[MAX_TURTLE][MAX_NAME], newTurt[MAX_TURTLE][MAX_NAME]; int incode[MAX_TURTLE]; int nCase, nTurt; cin >> nCase; int i; for (i=0; i<nCase; i++) { cin >> nTurt; cin.get(); input_turtle(oldTurt, nTurt); input_turtle(newTurt, nTurt); incoding(oldTurt, newTurt, nTurt, incode); bool isMove[MAX_TURTLE] = {0,}; move_turtle(incode, nTurt, isMove); print_turtle(newTurt, incode, isMove, nTurt); if (i != nCase-1) cout << endl; } return 0; } void input_turtle(char turt[][MAX_NAME], const int & nTurt) { int i; for (i=0; i<nTurt; i++) cin.getline(turt[i], MAX_NAME, '\n'); } void incoding(const char oldTurt[][MAX_NAME], const char newTurt[][MAX_NAME], const int & nTurt, int * code) { int i, j; for (i=0; i<nTurt; i++) { for (j=0; j<nTurt; j++) { if (strcmp(newTurt[i], oldTurt[j]) == 0) { code[j] = i; break; } } } } void move_turtle(const int * code, const int & nTurt, bool * isMove) { int i; int count = nTurt-1; if (code[count] != count) isMove[count] = true; else count--; for (i=nTurt-2; i>=0; i--) { if (code[i] != count) isMove[i] = true; else count--; } } void print_turtle(const char turt[][MAX_NAME], const int * code, const bool * isMove, const int & nTurt) { int i, j; for (i=nTurt-1; i>=0; i--) { for (j=0; j<nTurt; j++) { if (isMove[j] && code[j] == i) { show_turtle(turt[code[j]]); break; } } } }