Sicily 1133. SPAM

题目地址:1133. SPAM

思路:

     题目意思是说在‘@’的前后出现题目给定的合法字符或者不连续出现‘.’字符的话,这个就是合理的输出。

     那么以@为中心,向前,向后扫描,当扫描到不符合字符时,记录此时位置,以前后为区间,输出字符。

     具体代码如下:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 bool is_ok(char ch) {
 6     if ((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z') ||
 7         (ch >= '0'&&ch <= '9') || ch == '-'||ch == '_') {
 8            return true;
 9     }
10     return false;
11 }
12 
13 int main() {
14     string test;
15     while (getline(cin, test)) {
16         if (test.size() == 0) continue;
17         for (int i = 1; i < test.size()-1; i++) {
18             if (test[i] == '@'&&is_ok(test[i-1])&&is_ok(test[i+1])) {
19                 int begin, end;
20                 for (begin = i-1; begin >= 0; begin--) {
21                     if ((test[begin] == '.'&&test[begin+1] == '.')) {
22                         break;
23                     }
24                     if (test[begin] != '.'&&!is_ok(test[begin])) {
25                         break;
26                     }
27                 }
28                 if (test[begin+1] == '.') begin++;
29                 for (end = i+1; end < test.size(); end++) {
30                     if ((test[end] == '.'&&test[end-1] == '.')) {
31                         break;
32                     }
33                     if (test[end] != '.'&&!is_ok(test[end])) {
34                         break;
35                     }
36                 }
37                 if (test[end-1] == '.') end--;
38                 for (int j = begin+1; j <= end-1; j++) {
39                     cout << test[j];
40                 }
41                 cout << endl;
42             }
43         }
44      }
45     
46     return 0;
47 }

 

posted @ 2014-11-15 00:57  winray  阅读(645)  评论(0编辑  收藏  举报