Sicily 1133. SPAM

For study!

Posted by Winray on March 4, 2016
  • 思路:
    • 题目意思是说在‘@’的前后出现题目给定的合法字符或者不连续出现‘.’字符的话,这个就是合理的输出。那么以@为中心,向前,向后扫描,当扫描到不符合字符时,记录此时位置,以前后为区间,输出字符。
  • 具体代码如下:
#include <iostream>
#include <string>
using namespace std;

bool is_ok(char ch) {
    if ((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z') ||
        (ch >= '0'&&ch <= '9') || ch == '-'||ch == '_') {
           return true;
    }
    return false;
}

int main() {
    string test;
    while (getline(cin, test)) {
        if (test.size() == 0) continue;
        for (int i = 1; i < test.size()-1; i++) {
            if (test[i] == '@'&&is_ok(test[i-1])&&is_ok(test[i+1])) {
                int begin, end;
                for (begin = i-1; begin >= 0; begin--) {
                    if ((test[begin] == '.'&&test[begin+1] == '.')) {
                        break;
                    }
                    if (test[begin] != '.'&&!is_ok(test[begin])) {
                        break;
                    }
                }
                if (test[begin+1] == '.') begin++;
                for (end = i+1; end < test.size(); end++) {
                    if ((test[end] == '.'&&test[end-1] == '.')) {
                        break;
                    }
                    if (test[end] != '.'&&!is_ok(test[end])) {
                        break;
                    }
                }
                if (test[end-1] == '.') end--;
                for (int j = begin+1; j <= end-1; j++) {
                    cout << test[j];
                }
                cout << endl;
            }
        }
     }
    
    return 0;
}