HackerRank - "Fraud Prevention"

Yes just an implementation problem.. a lot of typing work.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef unsigned long UL;
typedef unsigned long long ULL;

struct Rec
{
    UL        oid;
    string    did;
    string    email;
    string    street;
    string    city;
    string    state;
    string    zip;
    string    ccd;
};

void decode(const string &in, Rec &r)
{
    size_t len = in.size();

    int inx = 0;
    int i = 0;
    string buf;
    while (i < len)
    {
        char c = in[i];
        if (c == ‘,‘)
        {
            switch (inx)
            {
            case 0:
                {
                istringstream in(buf);
                UL ul_oid; in >> ul_oid;
                r.oid = ul_oid; break;
                }
            case 1:
                r.did = buf; break;
            case 2:
                r.email = buf; break;
            case 3:
                r.street = buf; break;
            case 4:
                r.city = buf; break;
            case 5:
                r.state = buf; break;
            case 6:
                r.zip = buf; break;
            }
            inx++, i++;
            buf.clear();
            continue;
        }
        buf += c;
        i++;
    }
    r.ccd = buf;
}

void trim(string &s)
{
    size_t len = s.length();

    int l = 0, r = 0;
    int i = 0;
    while (i < len)
    {
        if (isspace(s[i]))
            l++;
        else
            break;
        i++;
    }
    i = len - 1;
    while (i >= 0)
    {
        if (isspace(s[i]))
            r++;
        else break;
        i--;
    }
    s = s.substr(l, len - l - r);
}

void toLowerAll(string &s)
{
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
}

void reformat(Rec &r)
{
    trim(r.did);
    trim(r.email);    toLowerAll(r.email);

    //
    string em;
    int i = 0;
    size_t len = r.email.length();
    bool bAppend = true;
    while (i < len)
    {
        char c = r.email[i];
        if (c == ‘.‘)
        {
            i++;
            continue;
        }
        if (c == ‘+‘)    bAppend = false;
        if (c == ‘@‘)    bAppend = true;
        if (bAppend)
        {
            em += c;
        }
        i++;
    }
    r.email = em;

    //
    trim(r.street); toLowerAll(r.street);
    string::size_type fi;
    if ((fi = r.street.find("rd.")) != string::npos)
    {
        r.street.replace(fi, 3, "road");
    }
    if ((fi = r.street.find("st.")) != string::npos)
    {
        r.street.replace(fi, 3, "street");
    }

    trim(r.city);    toLowerAll(r.city);
    trim(r.state);    toLowerAll(r.state);
    if ((fi = r.state.find("illinois")) != string::npos)
    {
        r.state.replace(fi, strlen("illinois"), "il");
    }
    if ((fi = r.state.find("california")) != string::npos)
    {
        r.state.replace(fi, strlen("california"), "ca");
    }
    if ((fi = r.state.find("new york")) != string::npos)
    {
        r.state.replace(fi, strlen("new york"), "ny");
    }
    trim(r.zip);
    string nzip; i = 0;
    while(i < r.zip.length())
    {
        char c = r.zip[i];
        if(c == ‘-‘) break;
        nzip += c;
        i ++;
    }
    r.zip = nzip;

    trim(r.ccd);
}

int main()
{
    set<UL> ret;

    unordered_map<string, pair<string, set<UL>>> m1; // email   + did -> ccd, oids
    unordered_map<string, pair<string, set<UL>>> m2; // address + did -> ccd, oids

    int n; cin >> n;
    string dum; getline(cin, dum);
    while (n--)
    {
        string ln;    getline(cin, ln);
        Rec r;
        decode(ln, r);
        reformat(r);

        //    Rule 1
        string k1 = r.email + r.did;
        if(m1.find(k1) == m1.end())
        {
            set<UL> oids; oids.insert(r.oid);
            m1[k1].first = r.ccd;
            m1[k1].second.insert(r.oid);
        }
        else
        {
            if(m1[k1].first != r.ccd)
            {
                ret.insert(m1[k1].second.begin(), m1[k1].second.end());
                ret.insert(r.oid);
            }
            else
            {
                m1[k1].second.insert(r.oid);
            }
        }

        //    Rule 2
        string k2 = r.street + r.city + r.state + r.zip + r.did;
        if(m2.find(k2) == m2.end())
        {
            set<UL> oids; oids.insert(r.oid);
            m2[k2] = make_pair(r.ccd, oids);
        }
        else
        {
            if(m2[k2].first != r.ccd)
            {
                ret.insert(m2[k2].second.begin(), m2[k2].second.end());
                ret.insert(r.oid);
            }
            else
            {
                m2[k2].second.insert(r.oid);
            }
        }
    }

    for(auto it = ret.begin(); it != ret.end(); it ++)
    {
        if(it != ret.begin())
            cout << ",";
        cout << *it;
    }

    return 0;
}

时间: 2024-11-04 07:11:49

HackerRank - "Fraud Prevention"的相关文章

微软职位内部推荐-Software Development Engineer II_Commerce

微软近期Open的职位: Are you looking for a high impact project that involves processing of billions of dollars, hundreds of millions of transactions, and tens of millions of users worldwide, and has tremendous upside potential? Do you want to create the next

微软职位内部推荐-Senior Software Development Engineer_Commerce

微软近期Open的职位: Are you looking for a high impact project that involves processing of billions of dollars, hundreds of millions of transactions, and tens of millions of users worldwide, and has tremendous upside potential? Do you want to create the next

微软的win10关于用户隐私的协议

分享隐私是升级win10必要条件? 为啥微软突然对用户隐私变得如此感兴趣?连操作系统的钱都不挣了?难道真的从CIA或相关部门拿到了经费? 这个不是空穴来风的瞎猜,有微软官方的文件: 微软官方网站的链接 很多人升级或者安装的时候通常都是一路 Accpet.OK.NEXT--闭着眼睛点下去的.有谁仔细看了这些密密麻麻的条文? 涉及用户隐私的部分 Reasons We Share Personal Data We share your personal data with your consent o

6.21外刊打卡3

Hedge funds and artificial intelligence 对冲基金与人工智能 商业银行的风险对冲可以分为自我对冲和市场对冲两种情况: 1.所谓自我对冲是指商业银行利用资产负债表或某些具有收益负相关性质的业务组合本身所具有的对冲特性进行风险对冲. 2.市场对冲是指对于无法通过资产负债表和相关业务调整进行自我对冲的风险(又称残余风险),通过衍生产品市场进行对冲. 假如你在10元价位买了一支股票,这个股票未来有可能涨到15元,也有可能跌到7元.你对于收益的期望倒不是太高,更主要的

7.20 1

Hedge funds and artificial intelligence对冲基金与人工智能Return on AIAI 回报AI-driven hedge funds need human brains, too由 AI 操盘的对冲基金也离不开人脑ARTIFICIAL intelligence (AI) has already changed some activities, including parts of finance likefraud prevention, but not

尤金&#183;卡巴斯基:卡巴斯基实验室调查内网遭黑客攻击事件

猫宁!!! 尤金·卡巴斯基介绍: 尤金·卡巴斯基出生于黑海沿岸的新罗西斯克,父亲担任工程师,母亲是书库管理员.母亲常买许多数学杂志供其阅读.他在16岁就跳级进入密码.电信与计算机科学学院就读,从1980年代起便对于资料压缩及密码学有很深的研究,毕业后进入KGB担任密码解析的工作. 1989年10月,他的电脑感染了Cascade病毒,他成功的自行解毒,从此对于电脑病毒及资讯安全课题产生兴趣并深入研究.他的朋友得知了他这项兴趣,常协助他搜集病毒样本供他研究.在几个月后他写出一个扫毒的工具程序称为“-

19 BLOCKCHAIN-AS-A-SERVICE COMPANIES MAKING THE DLT MORE ACCESSIBLE

https://builtin.com/blockchain/blockchain-as-a-service-companies rising star in the business world, Blockchain-as-a-Service (BaaS) — when a third-party installs and maintains blockchain networks for a company’s technologies — helps businesses develop

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

hackerrank maxsum mod

https://www.hackerrank.com/challenges/maximise-sum/submissions/code/12028158 hackerrank 子数组和模m的最大值 时间复杂度nlgn, 主要是证明一点,presum[i]-presum[j] 对于0<j<i的j,最大的是第一个大于presum[i]的presum[j] 证明如下 Quro的讨论,感觉还是证明的过程不够细致 http://www.quora.com/What-is-the-logic-used-i