题目大意:
一个合法的
email
地址应该满足如下条件:
1.letter
为大写或小写字母;
2.symbol
为
letter
或数字或“-”,“_”;
3.word
由若干个
symbol
连接而成;
4.prefix
由若干个
word
连接而成,中间以“.”隔开;
5.domain
由两个或三个
letter
构成;
6.suffix=prefix.domain
;
7.address=prefix@suffix
;
给出
n
个字符串判断每个是否合法。
分析:
AC code:
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef double DB;
typedef long double LD;
using namespace std;
bool letter(char c)
{
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true;
return false;
}
bool symbol(char c)
{
if(letter(c) || (c >= '0' && c <= '9') || c == '_' || c == '-')
return true;
return false;
}
bool word(const string &s)
{
if(!s.size()) return false;
for(int i = 0, sz = s.size(); i < sz; ++i)
if(!symbol(s[i]))
return false;
return true;
}
bool prefix(const string &s)
{
if(!s.size()) return false;
int p = 0, q, l = s.size();
while((q = s.find_first_of('.', p)) != -1)
{
if(!word(s.substr(p, q-p))) return false;
p = q+1;
}
if(!word(s.substr(p, l-p))) return false;
return true;
}
bool domain(const string &s)
{
int l = s.size();
if(l == 2 || l == 3)
{
for(int i = 0; i < l; ++i)
if(!letter(s[i]))
return false;
return true;
}
else return false;
}
bool suffix(const string &s)
{
if(!s.size()) return false;
int p = s.find_last_of('.');
if(p == -1) return false;
return prefix(s.substr(0, p))&&domain(s.substr(p+1, s.size()-p-1));
}
bool check(const string &s)
{
int p = s.find('@');
if(p == -1 || s.find_first_of('@', p+1) != -1) return false;
return prefix(s.substr(0, p))&&suffix(s.substr(p+1, s.size()-p-1));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int Test;
string input;
scanf("%d\n", &Test);
while(Test--)
{
getline(cin, input);
if(check(input)) cout << "YES" << endl;
else cout << "NO" << endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}