sicily 1133. SPAM

   题目地址:http://soj.me/1133

   给出一大段字符串,然后找出其中的符合邮箱格式的字符串,并且输出,字符串可以重复利用,比如aa@aa@a有两个邮箱地址,分别是aa@aa,aa@a,我觉得这种题目还是很容易wrong的,一不小心就wrong了,大概思路就是把字符串从头到尾扫描,遇到@的时候,以@为中心向两边扫描,得到最大的邮箱地址为止,代码如下:

#include 
#include 
#include 
using namespace std;
bool isWordIllegal(char ch) {
  return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') ||
    (ch >= 'A' && ch <= 'Z') || ch == '-' || ch == '_');
}
vector vt;
void substract(string& str, int mid, int& start, int& end) {
  start = end = mid;
  int len = str.length();
  for (int i = mid+1; i < len; i++) {
    if (isWordIllegal(str[i]))
      end++;
    else if (str[i] == '.') {
      if (i >= len)
        break;
      else if (isWordIllegal(str[i+1]))
        end++;
      else
        break;
    } else
      break;
  }
  for (int i = mid - 1; i >= 0; i--) {
    if (isWordIllegal(str[i]))
     start--;
    else if (str[i] == '.') {
      if (i <= 0)
        break;
      else if (isWordIllegal(str[i-1]))
        start--;
      else
        break;
    } else
      break;
  }
  return;
} 
void solve(string& s) {
  int len = s.length();
  for (int i = 0; i < len; i++) {
    if (s[i] == '@') {
      int start, end;
      substract(s, i, start, end);
      string temp = s.substr(start, end-start+1);
      if (start != i && end != i)
        vt.push_back(temp);
    }
  }
}

int main() {
  string s;
  int start, end;
  while (getline(cin, s)) {
    solve(s);
  }
  int len = vt.size();
  for (int i = 0; i < vt.size(); i++)
    cout << vt[i] << endl;
  //system("pause");
  return 0;
}


你可能感兴趣的:(Sicily)