#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
 public:
  int numUniqueEmails(vector<string>& emails) {
    string::iterator it;
    cout << "Normalising the addresses:"<<endl;
    for (int i = 0; i < emails.size(); i++) {
      cout<<"  "<<emails[i]<<"->";
      for (it = emails[i].begin(); it!=emails[i].end() && *it != '@'; it++) {
      	cout<<*it; //ouch
        if (*it == '.') {
          emails[i].erase(it);
          it--;
        }
        if (*it == '+') {
          while (it!=emails[i].end() && *it != '@') {
            emails[i].erase(it);
          }
          break;
        }
      }
      cout<<"->"<<emails[i]<<endl; //ouch
    }
    cout << "Sorting."<<endl; 
    sort(emails.begin(), emails.end());
    cout << "Remove dups."<<endl; 
    for (int j = 0; j < emails.size()-1; j++) {
        if (emails[j] == emails[j + 1]) {
          emails.erase(emails.begin() + j);
          j--;
        }
    }
    
    cout << "Sorted vector:"<<endl; 
    for (auto &x:emails) 
        cout<<"  "<<x<<endl;
    return emails.size();
  }
};


int main() {
	//vector<string> v { "hello@x.y", "darkness@y.z", "my.old.friend@z.w" }; 
	//vector<string> v { "hello@x.y", "darkness@y.z", "hello@x.y" }; 
	//vector<string> v { "hel.lo@x.y", "darkness@y.z", "hello@x.y", "my.old.friend@z.w","myoldfriend@z.w" }; 
	vector<string> v { "", "hello", "darkness@", "hello@xy.", "dark.ness@" }; 
	Solution s; 
	cout << s.numUniqueEmails(v)<<endl; 
	
	return 0;
}