Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <cctype>
- std::string to_lower(const std::string &s)
- {
- std::string data(s);
- std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c) { return std::tolower(c); });
- return data;
- }
- bool is_grammar(const std::multimap<std::string, std::string> &dict)
- {
- if (dict.empty() || dict.count(std::string("S")) == 0) {
- return false;
- }
- for (auto &c:dict) {
- if (c.first == to_lower(c.first)) {
- return false;
- }
- }
- return true;
- }
- bool is_context_free(const std::multimap<std::string, std::string> &dict)
- {
- for (auto &c:dict) {
- if (c.first.size() != 1 || c.first[0] != std::toupper(c.first[0])) {
- return false;
- }
- }
- return true;
- }
- bool is_inconclusive_context_free(const std::multimap<std::string, std::string> &dict)
- {
- bool flag1 = false;
- bool flag2 = false;
- if (!is_context_free(dict)) {
- return false;
- }
- for (auto &c:dict) {
- if (c.second.find("S") != std::string::npos) {
- flag1 = true;
- }
- if (c.second == "_") {
- if (c.first != "S") {
- return false;
- } else {
- flag2 = true;
- }
- }
- }
- return !(flag1 && flag2);
- }
- int main()
- {
- std::multimap<std::string, std::string> dict;
- std::string key, value;
- while (std::cin >> key >> value) {
- dict.insert({key, value});
- }
- if (!is_grammar(dict)) {
- std::cout << "-1" << std::endl;
- } else {
- if (is_inconclusive_context_free(dict)) {
- std::cout << "23" << std::endl;
- } else if (is_context_free(dict)) {
- std::cout << "2" << std::endl;
- } else {
- std::cout << "10" << std::endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment