D.Cell Phone Contacts (UCF “Practice” Local Contest — Aug 24, 2013 )

模拟就好,但是注意细节,运用了几个stl。

思路:先记录下所有人的名字,然后再用名字在排完序的数组中的序号去找名字对应的emal和number,再对这俩排序。

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
struct Node
{
    string first;
    string last;
};
string s[109][3];
vector<Node> v;
vector<string> em[109];
vector<string> ph[109];
map<string, int> mp;
int T, n;
bool cmp(Node a, Node b)
{
    if (a.last != b.last)
        return a.last < b.last;
    else
        return a.first < b.first;
}
bool cmp1(string a, string b)
{
    if (a.size() != b.size())
        return a.size() < b.size();
    else
        return a < b;
}
bool cmp2(string a, string b)
{
    return a < b;
}
bool isemal(string s)
{
    for (int i = 0; i < s.size(); i++)
        if (s[i] == '@')
            return true;
    return false;
}
void Get(int p)
{
    for (int i = 0; i < n; i++)
        if (v[p].last == s[i][1] && v[p].first == s[i][0])
        {
            if (isemal(s[i][2]))
                em[p].push_back(s[i][2]);
            else
                ph[p].push_back(s[i][2]);
        }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    int kase = 0;
    while (cin >> n && n)
    {
        v.clear();
        mp.clear();
        for (int i = 0; i < 100; i++)
        {
            em[i].clear();
            ph[i].clear();
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cin >> s[i][j];
                if (j == 1)
                {
                    string t = s[i][0] + s[i][1];
                    if (mp[t] == 0)
                    {
                        v.push_back({s[i][0], s[i][1]});
                        mp[t] = 1;
                    }
                }
            }
        }
        printf("Contact list #%d:\n", ++kase);
        sort(v.begin(), v.end(), cmp);
        for (int i = 0; i < v.size(); i++)
        {
            Get(i);
            sort(ph[i].begin(), ph[i].end(), cmp1);
            sort(em[i].begin(), em[i].end(), cmp2);
            cout << v[i].first << " " << v[i].last << endl;
            cout << "Phone:" << endl;
            for (int j = 0; j < ph[i].size(); j++)
            {
                string t = ph[i][j];
                for (int k = 0; k < t.size(); k++)
                {
                    if (k == 0)
                        printf("(");
                    cout << t[k];
                    if (k == 2)
                        printf(")");
                    if (k == 5)
                        printf("-");
                }
                cout << endl;
            }
            cout << "E-Mail:" << endl;
            for (int j = 0; j < em[i].size(); j++)
            {
                cout << em[i][j] << endl;
            }
            printf("###\n");
        }
        printf("\n");
    }
    return 0;
}
发布了81 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/104818372