abc187_c - 1 Sat

Check for existence of $x$ and $!x$.

Time complexity: $O(n)$

Memory complexity: $O(n \log{n})$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n;
    cin >> n;
    string x;
    bool satisfiable = true;
    map<string, int> is_positive;
    for (int i = 0; i < n; ++i)
    {
        string s;
        int sign = +1;
        char ch = (std::cin >> std::ws).peek();
        if (ch == '!')
        {
            cin >> ch;
            sign = -1;
        }
        cin >> s;
        if (is_positive.find(s) == is_positive.end())
            is_positive[s] = sign;
        else if (is_positive[s] != sign)
        {
            satisfiable = false;
            x = s;
        }
    }
    cout << (satisfiable ? "satisfiable" : x) << endl;
    return 0;
}