sicily 1133. SPAM

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

   給出一大段字符串,然後找出其中的符合郵箱格式的字符串,並且輸出,字符串可以重複利用,比如aa@aa@a有兩個郵箱地址,分別是aa@aa,aa@a,我覺得這種題目還是很容易wrong的,一不小心就wrong了,大概思路就是把字符串從頭到尾掃描,遇到@的時候,以@爲中心向兩邊掃描,得到最大的郵箱地址爲止,代碼如下:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isWordIllegal(char ch) {
  return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') ||
    (ch >= 'A' && ch <= 'Z') || ch == '-' || ch == '_');
}
vector<string> 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章