PAT中的哈希表

PAT1048:找硬币

哈希表

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

unordered_set<int> S;
int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    int r1 = 1e9, r2 = 0;
    for (int i = 0; i < n; i ++ )
    {
        int x;
        scanf("%d", &x);
        int y = m - x;
        if(S.count(y))
        {
            if(x > y) swap(x, y);
            if(x < r1) r1 = x, r2 = y;
        }
        S.insert(x);
    }
    if(r1 == 1e9) puts("No Solution");
    else printf("%d %d\n", r1, r2);
    
    return 0;
}

排序+双指针(时间略快)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 100010;
int n, m;
int a[N];
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )
        scanf("%d", &a[i]);
        
    sort(a, a + n);
    
    int l = 0, r = n - 1;
    while (l < r)
    {
        int x = a[l] + a[r];
        if(x == m)
        {
            printf("%d %d\n", a[l], a[r]);
            return 0;
        }
        else if(x > m) r --;
        else l ++;
    }
    printf("No Solution\n");
    return 0;
}

PAT1063:集合相似度

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int N = 55;
int n, m;
unordered_set<int> S[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
    {
        int x;
        scanf("%d", &x);
        while (x -- )
        {
            int y;
            scanf("%d", &y);
            S[i].insert(y);
        }
    }
    scanf("%d", &m);
    while (m -- )
    {
        int a, b;
        scanf("%d %d", &a, &b);
        int nc = 0, nt = 0;
        for (auto &t: S[a])
        {
            nc += S[b].count(t);
        }
        nt = S[a].size() + S[b].size() - nc;
        printf("%.1lf%%\n", double(nc) / nt * 100.0);
    }
    return 0;
}

PAT1120:朋友数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;
set<int> S;
int count(int x)
{
    int res = 0;
    while (x)
    {
        res += x % 10;
        x /= 10;
    }
    return res;
}
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
    {
        int x;
        scanf("%d", &x);
        S.insert(count(x));
    }
    vector<int> f;
    for (auto &t: S)
        f.push_back(t);
    printf("%d\n", S.size());
    printf("%d", f[0]);
    for (int i = 1; i < f.size(); i ++ )
        printf(" %d", f[i]);
    return 0;
}

PAT1144:漏掉的数字

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

int main()
{
    int n;
    unordered_set<int> S;
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
    {
        int x;
        scanf("%d", &x);
        S.insert(x);
    }
    for (int i = 1; ; i ++ )
    {
        if(!S.count(i))
        {
            printf("%d\n", i);
            break;
        }
    }
    return 0;
}

PAT1149:危险集装箱

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int N = 100010;
int n, m;
int a[N], b[N];
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d %d", &a[i], &b[i]);
    
    while (m -- )
    {
        int k;
        scanf("%d", &k);
        unordered_set<int> S;
        while (k --)
        {
            int x;
            scanf("%d", &x);
            S.insert(x);
        }
        bool success = true;
        for (int i = 0; i < n; i ++ ) 
            if(S.count(a[i]) && S.count(b[i]))
            {
                success = false;
                break;
            }
        if(success) puts("Yes");
        else puts("No");
    }
    return 0;
}

PAT1078:哈希(开放寻址法)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 100010;
int n, m;
bool f[N];
bool is_prime(int x)  // 判定质数
{
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i ++ )
        if (x % i == 0)
            return false;
    return true;
}
int find(int x)
{
    int t = x % m;
    
    for (int k = 0; k < m; k ++ )
    {
        int i = (t + k * k) % m;
        if(!f[i]) return i;
    }
    return -1;
}
int main()
{
    scanf("%d%d", &m, &n);
    while (!is_prime(m)) m ++;
    for (int i = 0; i < n; i ++ )
    {
        if(i) printf(" ");
        int x;
        scanf("%d", &x);
        int t = find(x);
        
        if(t == -1) printf("-");
        else printf("%d", t);
        
        f[t] = true;
    }
    return 0;
}

PAT1145:哈希——平均查找时间

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 100010;
int p, n, m;
int f[N];
bool is_prime(int x)
{
    if(x < 2) return false;
    for (int i = 2; i <= x / i; i ++ )
        if(x % i == 0)
            return false;
            
    return true;
}
int find(int x, int &cnt)
{
    int t = x % p;
    cnt = 1;
    for (int k = 0; k < p; k ++ )
    {
        int i = (t + k * k) % p;
        if(!f[i] || f[i] == x) return i;
        cnt ++;
    }
    return -1;
}
int main()
{
    scanf("%d %d %d", &p, &n, &m);
    
    while (!is_prime(p)) p ++;
    
    for (int i = 0; i < n; i ++ )
    {
        int x, count = 0;
        scanf("%d", &x);
        int t = find(x, count);
        
        if(t == -1) printf("%d cannot be inserted.\n", x);
        else f[t] = x;
    }
    // for (int i = 0; i < p; i ++ ) printf("%d ", f[i]);
    int cnt = 0;
    for (int i = 0; i < m; i ++ )
    {
        int x, count = 0;
        scanf("%d", &x);
        int t = find(x, count);
        cnt += count;
    }
    
    printf("%.1lf", (double)cnt / m);
    return 0;
}

PAT1137:期终成绩

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cmath>

using namespace std;

struct Student
{
    string id;
    int p, m, f, s;
    
    Student(): p(-1), m(-1), f(-1), s(0) {}
    
    void calc()
    {
        if(f >= m) s = f;
        else s = round(m * 0.4 + f * 0.6);
    }
    
    bool operator< (const Student& t) const
    {
        if(s != t.s) return s > t.s;
        else return id < t.id;
    }
};

int main()
{
    int p, m, f;
    cin >> p >> m >> f;
    
    string id;
    int x;
    unordered_map<string, Student> hash;
    for (int i = 0; i < p; i ++ )
    {
        cin >> id >> x;
        hash[id].id = id;
        hash[id].p = x;
    }
    
    for (int i = 0; i < m; i ++ )
    {
        cin >> id >> x;
        hash[id].id = id;
        hash[id].m = x;
    }
    
    for (int i = 0; i < f; i ++ )
    {
        cin >> id >> x;
        hash[id].id = id;
        hash[id].f = x;
    }
    
    vector<Student> students;
    for (auto &stu:hash)
    {
        auto t = stu.second;
        
        t.calc();
        if(t.p >= 200 && t.s >= 60) students.push_back(t);
    }
    
    sort(students.begin(), students.end());
    
    for (auto &t : students)
        cout << t.id << ' ' << t.p<< ' ' << t.m << ' ' << t.f << ' ' << t.s << endl;
    return 0;
}

PAT1013:战争中的城市

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1010, M = 500010;
int n, m, k;
int p[N];
struct Edge
{
    int a, b;
}edge[M];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    cin >> n >> m >> k;
    int x, y;
    for (int i = 0; i < m; i ++ )
    {
        scanf("%d %d", &x, &y);
        edge[i].a = x, edge[i].b = y;
    }
    
    for (int i = 0; i < k; i ++ )
    {
        int x;
        scanf("%d", &x);
        
        for (int i = 1; i <= n; i ++ ) p[i] = i;
        
        int cnt = n - 1;
        for (int i = 0; i < m; i ++ )
        {
            int a = edge[i].a, b = edge[i].b;
            if(a == x || b == x) continue;
            int pa = find(a), pb = find(b);
            if(pa != pb)
            {
                p[pa] = pb;
                cnt --;
            }
        }
        
        printf("%d\n", cnt - 1);
    }
    return 0;
}

PAT1118:森林中的鸟

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int N = 10010;
int n, m, k;
int p[N];
int bird[15];
int find(int x)  // 并查集
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i < N; i ++ ) p[i] = i;
    unordered_set<int> S;
    int cnt = 0;
    for (int i = 0; i < n; i ++ )
    {
        int k;
        scanf("%d", &k);
        for (int i = 0; i < k; i ++ )
        {
            scanf("%d", &bird[i]);
            S.insert(bird[i]);
        }
        for (int i = 0; i < k - 1; i ++ )
        {
            int a = bird[i], b = bird[i + 1];
            if(find(a) != find(b))
            {
                p[find(a)] = find(b);
                cnt ++;
            }
        }
    }
    printf("%d %d\n", S.size() - cnt, S.size());
    scanf("%d", &m);
    for (int i = 0; i < m; i ++ )
    {
        int x, y;
        scanf("%d %d", &x, &y);
        if(find(x) != find(y)) puts("No");
        else puts("Yes");
    }
    return 0;
}

PAT1107:社会集群

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int N = 1010;
int n;
int p[N], cnt[N];
vector<int> hobby[N];
int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
    {
        int k;
        scanf("%d:", &k);
        while (k -- )
        {
            int x;
            scanf("%d", &x);
            hobby[x].push_back(i);
        }
    }
    
    for (int i = 0; i < n; i ++ ) p[i] = i;
    
    for (int i = 1; i <= 1000; i ++ )
    {
        for (int j = 1; j < hobby[i].size(); j ++ )
        {
            int a = hobby[i][0], b = hobby[i][j];
            p[find(b)] = find(a);
        }
    }
    
    for (int i = 0; i < n; i ++ ) cnt[find(i)] ++;
    
    sort(cnt, cnt + n, greater<int>());
    
    int k = 0;
    while (cnt[k]) k ++;

    cout << k << endl;
    
    cout << cnt[0];
    for (int i = 1; i < k; i ++ )
        printf(" %d", cnt[i]);
    cout << endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值