題目地址: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 }