// The Tables. // Mr. and Mrs. Tables hold the occasional party, and they are // rather strict about the sitting arrangements. They decide // to jump on the automation band-wagon and hire you to write // a program to figure out their sitting arrangements. // Your job is to read their requirements on the sitting arrangements // and see if particular arrangements conform to their // requirements or not. // // In their simple parties, guests sit from left to right, on one // side of a straight table. Requirements are of the format // . Each is a letter of alphabet, // capitalized, starting with A being the first guest. // is a single character, one of '<', '^', '$', or '>'. // // AB means B sits to the right of B // ^A means A sits at the left end of the table // A$ means A sits at the right end of the table // // Each particular sitting arrangement is specified simply by // listing the guests from left to right, e.g. ACDEF // You may assume that every guest shows up at the party // // Input: Your input will start with two numbers n <= 26 and m. n is the // number of guests present at the party, and m is the number // of requirements the Tables have on the arrangements. // This will be followed by m lines each specifying a requirement. // Following that there will be any number of specific arrangements, // given as described above. // // Output: For each specific sitting arrangement specified in input, // print out one line with 'TRUE' if the arrangement // matches the requirements or 'FALSE' if it violates // one of the requirements. // #include #include #include #include using namespace std; map mustSitLeftOf; char leftEnd = '-', rightEnd = '-'; void readRequirements(int ruleCount); bool evaluateArrangement(string arrangement); int main() { int n, m; string arrangement; cin >> n >> m; // number of guests, requirements. readRequirements(m); while (cin >> arrangement) cout << (evaluateArrangement(arrangement) ? "TRUE" : "FALSE") << endl; return 0; } void readRequirements(int count) { string str; char guest1, guest2; for (; count > 0; count--) { cin >> str; if (sscanf(str.c_str(), "^%c", &guest1) == 1) leftEnd = guest1; else if (sscanf(str.c_str(), "%c<%c", &guest1, &guest2) == 2) mustSitLeftOf[guest1] = guest2; else if (sscanf(str.c_str(), "%c>%c", &guest1, &guest2) == 2) mustSitLeftOf[guest2] = guest1; else if (sscanf(str.c_str(), "%c$", &guest1) == 1) rightEnd = guest1; } } bool evaluateArrangement(string arrangement) { int length = arrangement.length(); // quick check on the two ends: if (leftEnd != '-' && arrangement[0] != leftEnd) return false; if (rightEnd != '-' && arrangement[length - 1] != rightEnd) return false; // by now, we know arrangement[0] is permitted to be // sitting at leftEnd, just check everyone compared to right for (int i = 1; i < length; i++) if (mustSitLeftOf.count(arrangement[i-1]) && mustSitLeftOf[arrangement[i-1]] != arrangement[i]) return false; return true; }