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

题目大意:
       一个合法的 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值