p274.cpp
======================
#include <bits/stdc++.h>
using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<string> emails;

bool is_symbol(char ch) {
    return isalpha(ch) || isdigit(ch) || ch == '_' || ch == '-';
}

bool is_valid_prefix(const string& prefix) {
    if(prefix.empty() || prefix.front() == '.' || prefix.back() == '.') {
        return false;
    }
    bool last_char_was_dot = false;
    for(char ch: prefix) {
        if(ch == '.') {
            if(last_char_was_dot) {
                return false;
            }
            last_char_was_dot = true;
        } else if(!is_symbol(ch)) {
            return false;
        } else {
            last_char_was_dot = false;
        }
    }
    return true;
}

bool is_valid_domain(const string& domain) {
    if(domain.size() != 2 && domain.size() != 3) {
        return false;
    }
    for(char ch: domain) {
        if(!isalpha(ch)) {
            return false;
        }
    }
    return true;
}

bool is_valid_suffix(const string& suffix) {
    size_t pos = suffix.rfind('.');
    if(pos == string::npos) {
        return false;
    }
    string prefix = suffix.substr(0, pos);
    string domain = suffix.substr(pos + 1);
    return is_valid_prefix(prefix) && is_valid_domain(domain);
}

bool is_valid_email(const string& email) {
    size_t pos = email.find('@');
    if(pos == string::npos) {
        return false;
    }
    string prefix = email.substr(0, pos);
    string suffix = email.substr(pos + 1);
    return is_valid_prefix(prefix) && is_valid_suffix(suffix);
}

void read() {
    cin >> n;
    cin.ignore();
    emails.resize(n);
    for(auto& email: emails) {
        getline(cin, email);
    }
}

void solve() {
    vector<string> results;
    for(const auto& email: emails) {
        results.push_back(is_valid_email(email) ? "YES" : "NO");
    }
    for(const auto& result: results) {
        cout << result << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}

=================
statement.txt
======================
274. Spam-filter
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



After tremendous last-year success not yet well-known, but already grown-up company H&H decided to create a new software masterpiece -- a world-leading spam-filter.
Due to Your poor last year -- a world-leading spam-filter. Due to your poor last year project performance, you are given a small and boring task again.
Your task is to write a simple email-validator.

Valid email address can be described as follows:
<letter> ::= a|b|...|z|A|B|...|Z

<symbol> ::= <letter>|0|1|...|9|_|-

<word> ::= <symbol>|<symbol><word>

<prefix> ::= <word>|<prefix>.<word>

<domain> ::= <letter><letter>|<letter><letter><letter>

<suffix> ::= <prefix>.<domain>

<address> ::= <prefix>@<suffix>

Input
The first line of the input file contains integer number N (1 <= N <= 100) -- the number of email addresses to be checked. Each of the following N lines contains one email address. Email address is the non-empty sequence of characters with ASCII codes from 32 to 255 no more then 100 characters long.

Output
For each address from the input write a separate line with the word YES, if the email address is valid and NO if it is not.

Sample test(s)

Input
3
abc@abc
abc@abc.abc
_@-.ru

Output
NO
YES
YES
Author:	Ilya V. Elterman
Resource:	ACM ICPC 2004-2005, NEERC, Southern Subregional Contest
Date:	Saratov, October 7, 2004

=================
