Sicily 1133. SPAM, sicily1133spam
Question address: 1133. SPAM
Ideas:
The question means that the question is given a valid character or the '.' character does not appear consecutively before and after '@', which is a reasonable output.
Take @ as the center, scan forward and backward. When the scan reaches non-conforming characters, record the position at this time. The former and later are intervals and output characters.
The Code is as follows:
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 }