SDUT ACM OJ PTA 数据结构 查找

1.电话聊天狂人

#include <bits/stdc++.h>
using namespace std;

int main() 
{
	map<string, int> mp;//用图来存储数据,空间使用少
	int n, mx = -1;
	string s;
	cin >> n;
	n = 2 * n;//一行两个电话
	for (int i = 0; i < n; i++) 
	{
		cin >> s;
		mp[s]++;//让这个电话号通话次数+1
		mx = max(mx, mp[s]);//比较更新最大值
	}
	int flag = 1, count = 0;
	map<string, int>::iterator it = mp.begin();//迭代器
	for (; it != mp.end(); it++) 
	{
		//用flag来控制输出,只输出第一个最大值
		if (flag && it->second == mx) 
		{
			cout << it->first << " " << mx;
			flag = 0;
		}
		//之后的最大值只进行计数
		if (it->second == mx) 
		{
			count++;
		}
	}
	if (count > 1)	cout << " " << count;
	return 0;
}

2.两个有序序列的中位数

#include <bits/stdc++.h>
using namespace std;
//全读进来,排序,取数即可
int main() {
	int a[200005], n;
	cin >> n;
	n *= 2;
	for (int i = 0; i < n; i++) 
	{
		cin >> a[i];
	}
	sort(a, a + n);
	cout << a[(n - 1) / 2];
}

3.词频统计

#include <bits/stdc++.h>
using namespace std;
//自定义排序方式
bool cmp(pair<string, int> a, pair<string, int> b) {
	if (a.second != b.second)
		return a.second > b.second;
	else
		return a.first < b.first;
}
int main() 
{
	map<string, int> mp;//map用来存储  单词-次数  对
	vector<pair<string, int>> v;//把map放入vector容器用于排序
	string s = "";
	char ch;
	//分割单词
	while (~scanf("%c", &ch) && ch != '#') 
	{
		if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_') 
		{
			if (ch >= 'A' && ch <= 'Z') ch = ch - 'A' + 'a';
			if (s.size() < 15)		    s += ch;
		} 
		else 
		{
			if (s.size() > 0) 
			{
				mp[s]++;
				s.clear();
			}
		}
	}
	map<string, int>::iterator it = mp.begin();
	for (; it != mp.end(); it++)//把map放入vector
		v.push_back(make_pair(it->first, it->second));
	
	sort(v.begin(), v.end(), cmp);//排序
	
	cout << v.size() << endl;//输出一共多少个单词
	
	for (int i = 0; i < v.size() / 10; i++)//输出前百分之十单词
		cout << v[i].second << ":" << v[i].first << endl;
	
	return 0;
}

4.集合相似度

#include <bits/stdc++.h>
using namespace std;
//这道题目用set集合,和stl提供的集合计算函数
int main() {
	set<int> st[55];//set集合自动去重
	int n, m, z, a, b;
	double x, y;
	
	cin >> n;//n个集合
	
	for (int i = 1; i <= n; i++) 
	{//读入数据
		cin >> m;//当前集合m个数据
		for (int j = 0; j < m; j++) 
		{
			cin >> z;
			st[i].insert(z);
		}
	}
	cin >> m;//集合计算次数
	while (m--) 
	{
		set<int>st1;//定义一个新的集合保存计算出来的集合
		cin >> a >> b;//集合a和集合b
		
		//相似度,也就是交集,用set_intersection函数求交集
		//括号里依次是 a 集合的头和尾  b 集合的头和尾  目标集合的迭代器及起始位置
		set_intersection(st[a].begin(), st[a].end(), st[b].begin(), st[b].end(), inserter(st1, st1.begin()));
		x = st1.size();
		y = st[a].size() + st[b].size() - x;// a集合 +  b集合 相似部分用新集合大小去掉
		printf("%.2lf%\n", x * 100.0 / y);
	}
	return 0;
}

5.悄悄关注

#include <bits/stdc++.h>
using namespace std;

int main() 
{
	map<string, int> mp1, mp2;
	int n, m, x, sum = 0, flag = 0;
	string s;
	cin >> n;
	for (int i = 0; i < n; i++) 
	{//读入 被其关注的用户的ID
		cin >> s;
		mp1[s] = 1;
	}
	cin >> m;
	for (int i = 0; i < m; i++) 
	{//读入 用户ID和对该用户的点赞次数
		cin >> s >> x;
		mp2[s] = x;
		sum += x;
	}
	sum /= m;//点赞平均数
	map<string, int>::iterator it = mp2.begin();//迭代器
	for (; it != mp2.end(); it++) 
	{
		if (!mp1[it->first] && it->second > sum) 
		{//第一个map中找不到,并且第二个map中找到的点赞数>点赞平均数
			cout << it->first << endl;
			flag = 1;//标记有
		}
	}
	if (!flag)cout << "Bing Mei You" << endl;
	return 0;
}

6.单身狗!????

#include <bits/stdc++.h>
using namespace std;

int main() {
	map<int, int> mp;
	set<int> st, st1;
	int n, m, x, y;
	cin >> n;
	for (int i = 0; i < n; i++) 
	{//已知夫妻/伴侣
		cin >> x >> y;
		mp[x] = y;
		mp[y] = x;
	}
	cin >> m;
	for (int i = 0; i < m; i++) 
	{//代判定ID
		cin >> x;
		st.insert(x);//set去重
	}
	int flag = 0;
	set<int>::iterator it;//迭代器
	for (it = st.begin(); it != st.end(); it++)
		if (st.end() == st.find(mp[*it]))
			//find查找函数,找不到会返回迭代器end()位置
			st1.insert(*it);
	
	cout<<st1.size()<<endl;
	
	for (it = st1.begin(); it != st1.end(); it++) 
	{//输出   单身狗  OvO!
		if (flag)
			cout << " ";
		printf("%05d", *it);
		flag = 1;
	}
	return 0;
}

7.词典

#include <bits/stdc++.h>
using namespace std;

int main() {
	map<string, string> mp;//map保存单词
	string s, st;
	int n, m;
	cin >> n >> m;
	while (n--) 
	{//读入
		cin >> s >> st;
		mp[st] = s;
	}
	while (m--) 
	{//判断
		cin >> s;
		if (mp[s] != "\0")
			cout << mp[s] << endl;
		else
			cout << "eh" << endl;
	}
	return 0;
}

8.中序遍历树并判断是否为二叉搜索树

二叉搜索树又称二叉排序树,具有以下性质:
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为二叉搜索树

#include <bits/stdc++.h>
using namespace std;
int g[1000][2], p[1000], idx;
void in(int x) 
{//中序遍历
	if (x) //如果不是0(有子结点)
	{
		in(g[x][0]);//继续遍历左边
		cout << x << endl;//输出结点
		p[++idx] = x;//放入数组
		in(g[x][1]);//继续遍历右边
	}
}
int main() 
{
	int n, m, x;
	cin >> n >> m;
	if (n == 0) 
	{//最小二叉搜索树
		cout << "Yes";
		return 0;
	}
	for (int i = 0; i < n - 1; i++) 
	{//读入数据并保存
		int r, e, d;
		cin >> r >> e >> d;
		g[r][e] = d;
	}
	in(m);
	int i;
	for (i = 1; i < n&&p[i] <= p[i + 1]; i++);
		if (i==n)	cout << "Yes";
		else		cout << "No";
	
	return 0;
}

不懂得可以私信我,随时解答。
程序有错误请私信我,以及时改正。感谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值