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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值