// @BEGIN_OF_SOURCE_CODE /* @JUDGE_ID: 17243NT 171 C++ */ // Send to judge@uva.es #include #include #include #include #ifdef ONLINE_JUDGE #define ins cin #define outs cout #else #define ins fin #define outs fout ifstream fin("myprog.in"); ofstream fout("myprog.out"); #endif #define MAXLEN 1000 bool findandreplace(char str[], char tofind[], char torep[]); bool removequote(char str[], char torep[]); void replaceall(char str[], char tofind[], char torep[]); bool valid(char message[]); int main() { int i = 0; char message[MAXLEN]; for(;;) { ins.getline(message, MAXLEN - 1, '\n'); if(strcmp(message, "#") == 0) break; i++; outs << setw(3) << i << ". "; if(valid(message)) outs << message << endl; else outs << "Trap!\n"; } return 0; } bool findandreplace(char str[], char tofind[], char torep[]) { char temp[MAXLEN]; int i, j, lenstr, lentofind; lenstr = strlen(str); lentofind = strlen(tofind); for(i = 0; i <= lenstr - lentofind; i++) { for(j = 0; j < lentofind; j++) { if(str[i + j] != tofind[j]) { break; } } if(j >= lentofind) break; } if(i > lenstr - lentofind) return false; strcpy(temp, &str[i + lentofind]); strcpy(&str[i], torep); strcat(&str[i], temp); return true; } bool removequote(char str[], char torep[]) { char temp[MAXLEN]; int i, j; for(i = 0; i < strlen(str); i++) if(str[i] == '\"') break; for(j = i + 1; j < strlen(str); j++) { if(str[j] >= '0' && str[j] <= '9') return false; if(str[j] == '\"') break; } if(j >= strlen(str)) return false; if(j == i + 1) return false; if(str[j - 1] == ' ') return false; strcpy(temp, &str[j + 1]); strcpy(&str[i], torep); strcat(&str[i + strlen(torep)], temp); return true; } void replaceall(char str[], char tofind[], char torep[]) { while(findandreplace(str, tofind, torep)) { } } bool valid(char message[]) { char edit[MAXLEN]; int i, nqt; replaceall(message, " ", " "); if(message[strlen(message) - 1] == ' ') message[strlen(message) - 1] = '\0'; nqt = 0; strcpy(edit, message); for(i = 0; i < strlen(edit); i++) { if(edit[i] >= 'A' && edit[i] <= 'Z') continue; if(edit[i] >= '0' && edit[i] <= '9') continue; if(edit[i] == ' ') continue; if(edit[i] == '.') continue; if(edit[i] == '\"') { nqt++; continue; } return false; } if(nqt % 2 == 1) return false; while(removequote(edit, "sign")) { } for(i = 0; i < strlen(edit); i++) if(edit[i] == '\"') return false; replaceall(edit, "0", "nnn"); replaceall(edit, "1", "nnn"); replaceall(edit, "2", "nnn"); replaceall(edit, "3", "nnn"); replaceall(edit, "4", "nnn"); replaceall(edit, "5", "nnn"); replaceall(edit, "6", "nnn"); replaceall(edit, "7", "nnn"); replaceall(edit, "8", "nnn"); replaceall(edit, "9", "nnn"); replaceall(edit, "nnnn", "nnn"); replaceall(edit, "CHANGE AVERAGE SPEED", "cas"); replaceall(edit, "CAS", "cas"); replaceall(edit, "cas TO nnn KMH", "change"); replaceall(edit, "RECORD TIME", "record"); replaceall(edit, "record", "time-keeping"); replaceall(edit, "change", "time-keeping"); replaceall(edit, "AT sign", "where"); replaceall(edit, "FIRST", "when"); replaceall(edit, "SECOND", "when"); replaceall(edit, "THIRD", "when"); replaceall(edit, "RIGHT", "direction"); replaceall(edit, "LEFT", "direction"); replaceall(edit, "GO when", "how"); replaceall(edit, "GO", "how"); replaceall(edit, "KEEP", "how"); replaceall(edit, "how direction where", "di-rectional"); replaceall(edit, "how direction", "di-rectional"); replaceall(edit, "di-rectional", "navigational"); replaceall(edit, "navigational AND THEN navigational", "navigational"); replaceall(edit, "navigational AND time-keeping", "instruction"); replaceall(edit, "navigational", "instruction"); replaceall(edit, "time-keeping", "instruction"); if(strcmp(edit, "instruction") == 0) return true; return false; } // @END_OF_SOURCE_CODE