21级数据结构与算法实验7——查找表

 

目录

7-1 电话聊天狂人

7-2 两个有序序列的中位数

7-3 词频统计

7-4 集合相似度

7-5 悄悄关注

7-6 单身狗

7-7 词典

7-8 这是二叉搜索树吗?

7-9 二叉搜索树


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;

可写为

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;

7-1 电话聊天狂人

给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。

输入格式:

输入首先给出正整数N(≤105),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。

输出格式:

在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。

输入样例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

输出样例:

13588625832 3
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
map<string, int>mp;
int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        string a, b;
        cin >> a >> b;
        mp[a] ++;
        mp[b] ++;
    }
    int Max = 0;
    string ans = "";
    for(auto it : mp)
    {
        if(it.second > Max)
        {
            Max = it.second;
            ans = it.first;
        }
        else if(it.second == Max && it.first < ans)
        {
            ans = it.first;
            Max = it.second;
        }
    }
    int cnt = 0;
    for(auto it : mp)
    {
        if(it.second == Max)
        {
            cnt ++;
        }
    }
    if(cnt > 1) cout << ans << " " << Max << " " << cnt << endl;
    else cout << ans << " " << Max << endl;
    return 0;
}

7-2 两个有序序列的中位数

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0​,A1​,⋯,AN−1​的中位数指A(N−1)/2​的值,即第⌊(N+1)/2⌋个数(A0​为第1个数)。

输入格式:

输入分三行。第一行给出序列的公共长度N(0<N≤100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的并集序列的中位数。

输入样例1:

5
1 3 5 7 9
2 3 4 5 6

输出样例1:

4

输入样例2:

6
-100 -10 1 1 1 1
-50 0 2 3 4 5

输出样例2:

1
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
vector<int>mp;
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        mp.push_back(x);
    }
    for(int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        mp.push_back(x);
    }
    sort(mp.begin(), mp.end());
    cout << mp[(mp.size() - 1)/2] << endl;
    return 0;
}

7-3 词频统计

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PSI pair<string, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
map<string, int>mp;
vector<PSI>v;
bool cmp(PSI a, PSI b)
{
    if(a.yy == b.yy)
        return a.xx < b.xx;
    return a.yy > b.yy;
}
int main()
{
    char s;
    string ss = "";
    while(~scanf("%c", &s) && s != '#')
    {
        bool flag = 1;
        if(s >= 'A'&&s <= 'Z')
            s += 32;
        if(s >= 'a'&&s <= 'z'||s>='0'&&s<='9'||s=='_')
        {
            if(ss.size() < 15) ss += s;
        }
        else
        {
            if(ss.size() > 0) mp[ss] ++, ss.clear();
        }
    }
    for(auto it : mp)
    {
        // cout << it.first << " " << it.second << endl;
        v.push_back({it.xx, it.yy});
    }
    sort(v.begin(), v.end(), cmp);
    int cnt = mp.size()/10;
    cout << mp.size() << endl;
    for(int i = 0; i < cnt; i ++)
    {
        cout << v[i].yy << ":" << v[i].xx << endl;
    }
    return 0;
}

7-4 集合相似度

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
set<int>v[1111];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
        int m;
        scanf("%d", &m);
        for(int j = 1; j <= m; j ++)
        {
            int x;
            scanf("%d", &x);
            v[i].insert(x);
        }
    }
    int k;
    scanf("%d", &k);
    while(k --)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        int cnt = 0;
        for(auto x : v[a])
        {
            if(v[b].find(x) != v[b].end())
            {
                cnt ++;
            }
        }
        int wao = (v[a].size() + v[b].size() - cnt);
        double ans = 1.0*cnt/wao;
        printf("%.2f%%\n", ans*100);
    }
    return 0;
}

7-5 悄悄关注

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PSI pair<string, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
map<string, int>mp;
vector<PSI>v;
vector<string>s;
int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        string s;
        cin >> s;
        mp[s] = 1;
    }
    cin >> n;
    int sum = 0;
    for(int i = 0; i < n; i ++)
    {
        string a;
        int b;
        cin >> a >> b;
        v.push_back({a, b});
        sum += b;
    }
    sum = sum/n;
    for(auto it : v)
    {
        if(it.second > sum && mp[it.xx] == 0)
            s.push_back(it.xx);
    }
    if(s.size() == 0)
    {
        puts("Bing Mei You");
        return 0;
    }
    sort(s.begin(), s.end());
    for(auto it : s)
        cout << it << endl;
    return 0;
}

7-6 单身狗

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
map<string, string>mp;
map<string, int>v;
vector<string>wao;
vector<string>ans;
int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        string a, b;
        cin >> a >> b;
        mp[a] = b;
        mp[b] = a;
        v[a] = 1;
        v[b] = 1;
    }
    cin >> n;
    while(n --)
    {
        string t;
        cin >> t;
        wao.push_back(t);
        v[t] ++;
    }
    for(auto it : wao)
    {
        if(v[it] == 2 && v[mp[it]] == 2)
            continue;
        ans.push_back(it);
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    n = ans.size();
    for(int i = 0; i < n; i ++)
    {
        cout << ans[i] << " \n"[i == n - 1];
    }
    return 0;
}

7-7 词典

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
map<string, string>mp;
int main()
{
    int n, m;
    cin >> n >> m;
    while(n --)
    {
        string a, b;
        cin >> a >> b;
        mp[b] = a;
    }
    while(m --)
    {
        string s;
        cin >> s;
        if(mp.find(s) == mp.end())
            puts("eh");
        else cout << mp[s] << endl;
    }
    return 0;
}

7-8 这是二叉搜索树吗?

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N];
vector<int>ans;
int mirror = 0;
void dfs(int l, int r)
{
    int i = l + 1, j = r;
    if(!mirror)
    {
        while(i<=r&&a[i]<a[l])i++;
        while(j>l&&a[j]>=a[l])j--;
    }
    else
    {
        while(i<=r&&a[i]>=a[l])i++;
        while(j>l&&a[j]<a[l])j--;
    }
    if(i != j + 1) return;
    dfs(l + 1, j);
    dfs(i, r);
    ans.push_back(a[l]);
}
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> a[i];
    dfs(1, n);
    if(ans.size() != n)
    {
        mirror = 1;
        ans.clear();
        dfs(1, n);
    }
    if(ans.size() != n)
        puts("NO");
    else
    {
        puts("YES");
        for(int i = 0; i < n; i ++)
        {
            cout << ans[i] << " \n"[i == n - 1];
        }
    }
    return 0;
}

7-9 二叉搜索树

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;

int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        int a;
        cin >> a;
        int x = a;
        int b = 1;
        while(a%2==0)
        {
            a /= 2;
            b *= 2;
        }
        b --;
        cout << x - b << " " << x + b << endl;
    }
    return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值