1001. SPAM

18 篇文章 0 订阅
13 篇文章 0 订阅

Description
You never had any friends, and don’t really want any anyways, and so you have decided to collect email addresses from web pages for direct e-mail advertising.
The text delivered to a web browser is usually marked up HTML, which may contain email addresses of the form:

user@server

¨ Both user and server are of the form alpha.numeric.with.dots. By alpha.numeric.with.dots, we mean a sequence of one or more characters which are alphabetic (A-Z,a-z), numeric (0-9), hyphens (-), underbars (_) and/or periods (.), with the following restrictions on periods:
n The sequence neither starts nor ends with a period.
n No periods are adjacent.
¨ Email addresses are preceded by the beginning of the file, or some character other than a letter (A-Z,a-z), digit (0-9), hyphen (-), or underbar (_).
¨ Email addresses are succeeded by the end of the file, or some character other than a letter (A-Z,a-z), digit (0-9), hyphen (-), or underbar (_).
¨ If the scanned text contains a sequence of the form

first@second@third
Then the output should contain first@second and second@third as email addresses. In a longer run, each pair split by an @-sign should appear as an email address in the output.
The point of this problem is to extract and record the email addresses embedded in other text.
Input
The input file will contain zero or more lines of ASCII text.
Output
Other than the standard leader and trailer, the output file has each email address found in the input file in the order it was found (duplicates not removed).
Sample Input
Copy sample input to clipboard
bob@banks.com wrote:
What does x=7 mean for this problem? For
example,

..a@a@aa@aaa@aaa..a@a@aa@aaa@aaa..a@a..@a…a@..@..

this scrolling @-example from jim@jones.com
Sample Output
bob@banks.com
a@a
a@aa
aa@aaa
aaa@aaa
a@a
a@aa
aa@aaa
aaa@aaa
a@a
jim@jones.com

我的代码

// Problem#: 19150
// Submission#: 4854439
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
using namespace std;

bool isValid(string& s, char c);

int main() {
    string s;
    while (cin >> s) {
        int first, second, third;
        first = -1;
        second = s.find("@", 1);
        if (second == string::npos || second == s.size() - 1)
            continue;
        third = s.find("@", second + 1);
        do {
            string left, right;

            left = s.substr(first + 1, second - first - 1);
            if (third == string::npos)
                right = s.substr(second + 1, s.size() - second);
            else right = s.substr(second + 1, third - second - 1);

            if (isValid(left, 'l') && isValid(right, 'r'))
                cout << left << "@" << right <<endl;
            first = second;
            second = third;
            third = s.find("@", third + 1);
        } while(second != string::npos);
    }
    return 0;
}

bool isValid(string& s, char c) {
    // 不能为空串 
    if (s.empty()) return false;

    // 字符合法 
    string valid = 
        "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-_.";

    // '.'处理
    if (c == 'l') {  // 左串
        // 字符合法 
        int index = s.find_last_not_of(valid);
        if (index == s.size() - 1) return false;
        else if (index != string::npos)
            s = s = s.substr(index + 1, s.size() - index - 1);

        if (s[s.size() - 1] == '.') return false;

        index = s.find_last_of(".");
        while (index > 0 && s[index - 1] != '.')
             index = s.find_last_of(".", index - 1);
        if (index != string::npos)
            s = s.substr(index + 1, s.size() - index - 1);
        if (s[0] == '.')
            s = s.substr(1, s.size() - 1);
        return true;
    } else if (c == 'r') {  // 右串 
        // 字符合法 
        int index = s.find_first_not_of(valid);
        if (index == 0) return false;
        else if (index != string::npos)
            s = s.substr(0, index);

        if (s[0] == '.') return false;

        // '.'不能毗连 
        index = s.find_first_of("..");
        while (index < s.size() - 1 && s[index + 1] != '.')
             index = s.find_first_of(".", index + 1);
        if (index != string::npos)
            s = s.substr(0, index);
        if (s[s.size() - 1] == '.')
            s = s.substr(0, s.size() - 1);
        return true;
    }
    return true;
}                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值