sgu274:Spam-filter(字符串模拟)

来源:互联网 发布:linux最常用的20个命令 编辑:程序博客网 时间:2024/06/12 22:42

题目大意:
      一个合法的email地址应该满足如下条件:
      1.letter为大写或小写字母;
      2.symbolletter或数字或“-”,“_”;
      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_pairtypedef 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;}
0 0
原创粉丝点击