Data structure and algorithm A search

1. Telephone chat madman

Given the call records of a large number of mobile phone users, find out the chat madman with the most calls.
Input format
The input first gives a positive integer N (≤ 105), which is the number of call records. Then N lines, each line gives a call record. For simplicity, only the 11 digit mobile phone numbers of the caller and the receiver are listed here, separated by spaces.
Output format
Give the mobile phone number of the chat madman and the number of calls in one line, separated by spaces. If such a person is not unique, the minimum number of madmen and the number of calls are output, and the number of parallel madmen is given.
sample input
4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832
sample output
13588625832 3
Code 1

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200005

char s[MAX][12];

int cmp(const void* a, const void* b)
{
	return strcmp((char*)a, (char*)b) > 0 ? 1 : -1;
}

int main()
{
	int n;
	int max = 0, num = 0;
	int d = -1;
	char arr[12] = "";
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s%s", s[i * 2], s[i * 2 + 1]);
	}
	qsort(s, 2 * n, sizeof(s[0]), cmp);
	for (int i = 1; i < 2 * n + 1; i++)
	{
		if (strcmp(s[i], s[i - 1]) == 0)
		{
			num++;
		}
		else
		{
			if (num > max)
			{
				strcpy(arr, s[i - 1]);
				max = num;
				d = 1;
			}
			else if (num == max)
			{
				d++;
			}
			num = 1;
		}
	}
	printf("%s %d", arr, max);
	if (d > 1)
	{
		printf(" %d", d);
	}
	return 0;
}

Code 2

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct loca loca;
typedef struct hash hash;

struct loca
{
	long long data;
	int c;
	loca* next;
};
struct hash
{
	int hsize;
	loca** har;
};
hash* Create(int n)
{
	hash* h = (hash*)malloc(sizeof(hash));
	h->hsize = n;
	h->har = (loca**)malloc(sizeof(loca*) * n);
	for (int i = 0; i < h->hsize; i++)
	{
		h->har[i] = (loca*)malloc(sizeof(loca));
		h->har[i]->next = NULL;
	}
	return h;
}
void insert(long long n, hash* h)
{
	int d = n % h->hsize;
	loca* pos = h->har[d];
	while (pos->next)
	{
		if (pos->next->data == n)
		{
			pos->next->c++;
			return;
		}
		pos = pos->next;
	}
	pos->next = (loca*)malloc(sizeof(loca));
	pos->next->data = n;
	pos->next->c = 1;
	pos->next->next = NULL;
}
void find(hash* h)
{
	long long d = 0;
	int max = 0, c;
	for (int i = 0; i < h->hsize; i++)
	{
		loca* p = h->har[i]->next;
		while (p)
		{
			if (p->c > max)
			{
				max = p->c;
				d = p->data;
				c = 0;
			}
			else if (p->c == max)
			{
				c++;
				if (p->data < d)
				{
					d = p->data;
				}
			}
			p = p->next;
		}
	}
	printf("%lld %d", d, max);
	if (c)
	{
		printf(" %d", c + 1);
	}
}
int isprime(int n)
{
	if (n == 2 || n == 3)
	{
		return 1;
	}
	if (n % 6 != 1 && n % 6 != 5)
	{
		return 0;
	}
	for (int i = 5; i * i <= n; i += 6)
	{
		if (n % i == 0 || n % (i + 2) == 0)
		{
			return 0;
		}
	}
	return 1;
}
int nextprime(int n)
{
	if (n % 2 == 0)
	{
		n++;
	}
	while (!isprime(n))
	{
		n += 2;
	}
	return n;
}
int main()
{
	int n;
	long long a, b;
	scanf("%d", &n);
	hash* h = Create(nextprime(n * 2));
	for (int i = 0; i < n; i++)
	{
		scanf("%lld %lld", &a, &b);
		insert(a, h);
		insert(b, h);
	}
	find(h);
	return 0;
}

2. Median of two ordered sequences

It is known that there are two equal length non descending sequences S1 and S2. Design the function to find the median of the union of S1 and S2. The median of the ordered sequences A0, A1,..., AN − 1 refers to the value of A(N − 1) / 2 ⌊, that is, the ⌊ (N+1)/2 ⌋ (A0 is the first number).
Input format
The input is divided into three lines. The first line gives the common length N of the sequence (0 < N ≤ 100000), and then input the information of a sequence in each line, that is, N integers arranged in non descending order. Numbers are separated by spaces.
Output format
Outputs the median of the union sequence of two input sequences in one row.
Input sample 1
5
1 3 5 7 9
2 3 4 5 6
Output sample 1
4
Input sample 2
6
-100 -10 1 1 1 1
-50 0 2 3 4 5
Output sample 2
1
code

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>

int arr1[100005], arr2[100005];
int n;

int Dw()
{
	int i = 0, j = 0;
	int m = (2 * n - 1) / 2;
	while (i + j < m)
	{
		if (arr1[i] > arr2[j])
		{
			j++;
		}
		else {
			i++;
		}
	}
	return arr1[i] > arr2[j] ? arr2[j] : arr1[i];
}

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr1[i]);
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr2[i]);
	}
	printf("%d", Dw());
	return 0;
}

3. Word frequency statistics [ unfinished ]

Please write a program to count the number of different words in an English text and the top 10% words with the highest word frequency.
The so-called "word" refers to a continuous string composed of no more than 80 word characters, but words longer than 15 will only intercept and retain the first 15 word characters. Legal "word characters" are upper and lower case letters, numbers and underscores, and other characters are considered word separators.
Input format
Input gives a non empty text that ends with a symbol # at the end. The input is guaranteed to have at least 10 different words.
Output format
Output the number of all different words in the text on the first line. Note that "words" are not case sensitive. For example, "pat" and "pat" are considered to be the same word.
Then, according to the decreasing order of word frequency, the top 10% words with the largest word frequency are output according to the format of word frequency: word. If there is juxtaposition, it will be output in ascending dictionary order.
sample input
This is a test.

The word "this" is the word with the highest frequency.

Longlonglonglongword should be cut off, so is considered as the same as longlonglonglonee. But this_8 is different than this, and this, and this...#
this line should be ignored.
sample output
(Note: Although the word the also appears 4 times, we only output the first 10% (i.e. the first 2 of the 23 words), and the is in the third place in alphabetical order, so we don't output it.)
23
5:this
4:is

code

4. Set similarity

Given two integer sets, their similarity is defined as Nc / Nt × 100%. Where Nc , is the number of unequal integers shared by both sets, and Nt , is the number of unequal integers shared by both sets. Your task is to calculate the similarity of any pair of given sets.
Input format
The first line of input gives a positive integer N (≤ 50), which is the number of sets. Then N rows, each corresponding to a set. Each set first gives a positive integer m (≤ 104), which is the number of elements in the set; Followed by M integers in the [0109] interval.
The next line gives a positive integer k (≤ 2000), and then K lines, each line corresponds to the number of a pair of sets for which similarity needs to be calculated (sets are numbered from 1 to N). Numbers are separated by spaces
Output format
For each pair of sets to be calculated, output their similarity in one line, which is the percentage number with 2 digits after the decimal point.
sample input
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
sample output
50.00%
33.33%
code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<set>

using namespace std;

int main()
{
	set<int>st[51];
	int n, m;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> m;
		while (m--)
		{
			int k;
			cin >> k;
			st[i].insert(k);
		}
	}
	int l;
	cin >> l;
	while (l--)
	{
		int a1, a2;
		int s = 0;
		cin >> a1 >> a2;
		set<int>::iterator it;
		for (it = st[a1].begin(); it != st[a1].end(); it++)
		{
			if (st[a2].count(*it))
			{
				s++;
			}
		}
		int ans = st[a1].size() + st[a2].size() - s;
		printf("%.2lf%%\n", s * 100.0 / ans);
	}
	return 0;
}


5. Quietly pay attention

There is a "silent attention" on Sina Weibo. A person who is quietly followed by a user does not appear on the user's attention list, but the system will push the microblog published by the person who is quietly followed to the user. Now let's be an Internet detective to pick out people who may be quietly followed by someone according to their attention list and their likes to other users.
Input format
Input: first, give a user's attention list in the first line, with the format as follows:
Number of people N user 1 user 2...... user N
Where N is a positive integer no more than 5000, and each user i (i=1,..., N) is the ID of the user concerned. It is a 4-bit string composed of numbers and English letters, and each item is separated by spaces.
Then give the user's praise information: first give a positive integer M no more than 10000, and then M lines. Each line gives a user ID and the number of likes to the user (no more than 1000), separated by spaces. Note: user ID is the unique identity of a user. The title ensures that there are no duplicate users in the attention list and in the like information.
Output format
We believe that the person who is liked by the user more than the average number of times and is not on his follow-up list is likely to be the person he quietly follows. According to this assumption, please output the user ID in ascending alphabetical order. It may be the person it quietly pays attention to, with 1 ID per line. If there is no such person, output "Bing Mei You".
Input sample 1
10 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao
8
Magi 50
Pota 30
LLao 3
Ammy 48
Dave 15
GAO3 31
Zoro 1
Cath 60
Output sample 1
Ammy
Cath
Pota
Input sample 2
11 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao Pota
7
Magi 50
Pota 30
LLao 48
Ammy 3
Dave 15
GAO3 31
Zoro 29
Output sample 2
Bing Mei You
code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

char s1[5000][5];
char s2[MAX][5];
int n2[MAX];
char s3[MAX][5];

int cmp(const void* a, const void* b)
{
	return strcmp((char*)a, (char*)b) > 0 ? 1 : -1;
}

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", s1[i]);
		fflush(stdin);
	}
	fflush(stdin);
	qsort(s1, n, sizeof(s1[0]), cmp);
	
	int m, k;
	int sum = 0;
	scanf("%d", &m);
	for (int i = 0; i < m; i++)
	{
		scanf("%s", s2[i]);
		fflush(stdin);
		scanf("%d", &n2[i]);
		sum += n2[i];
	}
	
	double ave = sum * 1.0 / m;
	int cnt = 0;
	for (int i = 0; i < m; i++)
	{
		if (ave < n2[i])
		{
			strcpy(s3[cnt++], s2[i]);
			for (int j = 0; j < n; j++)
			{
				if (s2[i][0] == s1[j][0])
				{
					if (strcmp(s2[i], s1[j]) == 0)
					{
						cnt--;
						break;
					}
				}
			}
		}
	}
	qsort(s3, cnt, sizeof(s3[0]), cmp);
	if (cnt > 0)
	{
		for (int i = 0; i < cnt - 1; i++)
		{
			printf("%s\n", s3[i]);
		}
		printf("%s", s3[cnt - 1]);
	}
	else
	{
		printf("Bing Mei You");
	}
	return 0;
}

6. Single dog

"Single dog" is a Chinese nickname for singles. Please find out the single guests from the large-scale party with tens of thousands of people in order to give special care.
Input format
The first line of input gives a positive integer N (≤ 50000), which is the logarithm of known husband and wife / partner; Then N lines, each line gives a couple / partner - for convenience, each person corresponds to an ID number, which is 5 digits (from 00000 to 99999), and the IDs are separated by spaces; Then give a positive integer m (≤ 10000), which is the total number of people attending the party; The next line gives the ID of the M guests, separated by spaces. No bigamy or stepping on two boats.
Output format
First, the first line outputs the total number of single guests; Then, the second line lists the single guests in order by increasing ID. IDS shall be separated by 1 space, and there shall be no extra space at the beginning and end of the line.
sample input
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
sample output
5
10000 23333 44444 55555 88888
code

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

const int N = 100005;

int arr[N];

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < N; i++)
	{
		arr[i] = -1;
	}
	for (int i = 0; i < n; i++)
	{
		int a1, a2;
		cin >> a1 >> a2;
		arr[a1] = a2;
		arr[a2] = a1;
	}
	int m;
	cin >> m;
	int cnt = 0;
	for (int i = 0; i < m; i++)
	{
		int k;
		cin >> k;
		if (arr[k] >= 0)
		{
			arr[k] = -2;
		}
		else if (arr[k] != -2)
		{
			arr[k] = -3;
			cnt++;
		}
	}
	for (int i = 0; i < N; i++)
	{
		if (arr[i] >= 0)
		{
			if (arr[arr[i]] == -2)
			{
				arr[arr[i]] = -3;
				cnt++;
			}
		}
	}
	cout << cnt << endl;
	for (int i = 0; i < N; i++)
	{
		if (arr[i] == -3)
		{
			printf("%05d", i);
			char c = --cnt == 0 ? '\n' : ' ';
			cout << c;
		}
	}
	return 0;
}

7. Dictionary

You have just moved from Waterloo to a big city where people speak an incomprehensible foreign language dialect. Fortunately, you have a dictionary to help you understand them.
Input format
The first line of input is positive integers n and M, followed by N lines of dictionary entries (up to 10000), and then M lines of foreign language words to be translated (up to 10000). Each dictionary entry contains an English word, followed by a space and a foreign language word. Each word in the input consists of up to 10 lowercase letters.
Output format
Output the translated English words, one word per line. Foreign words in non dictionary output "eh".
sample input
5 3
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
sample output
cat
eh
loops
code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10005

typedef struct
{
	char word[11];
	char fword[11];
}Enum;

int main()
{
	int n, m;
	Enum e[MAX];
	char tw[11];
	scanf("%d %d", &n, &m);
	fflush(stdin);
	for (int i = 0; i < n; i++)
	{
		scanf("%s %s", e[i].word, e[i].fword);
		fflush(stdin);
	}
	while (m--)
	{
		scanf("%s", tw);
		fflush(stdin);
		int i;
		int flag = 0;
		for (i = 0; i < n; i++)
		{
			if (e[i].fword[0] == tw[0])
			{
				if (strcmp(e[i].fword, tw) == 0)
				{
					printf("%s\n", e[i].word);
					flag = 1;
				}
			}
		}
		if (flag == 0)
		{
			printf("eh\n");
		}
	}
	return 0;
}

8. Traverse the tree in middle order and judge whether it is a binary search tree

For a given binary tree with N nodes (N > = 0), the middle order traversal sequence is given, and whether it is a binary search tree is judged.
The problem is to ensure that the binary tree does not exceed 200 nodes, and the node values are within the integer int range and are different.
Input format
The first line is a non negative integer N, indicating that there are N nodes
The second line is an integer k, which is the element value of the tree root
Next, there are N-1 lines. Each line is a new node in the format of three integers r d e,
r represents the element value of the parent node of the node (ensure the existence of the parent node); d is the direction, 0 indicates that the node is the left son of the parent node, and 1 indicates the right son; e is the element value of the node
Output format
Firstly, the middle order traversal sequence of binary tree is output, and each element occupies one line. For an empty tree, nothing is output.
Then, if the given tree is a binary search tree, output Yes, otherwise output No
sample input
For the binary tree in the picture:
3
20
20 0 10
20 1 25
sample output
10
20
25
Yes
code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define MAX 200

int a[205];
int i;
int flag;
int min;

typedef struct
{
	int data;
	struct Tree* lchild, * rchild;
}Tree;

Tree* init()
{
	Tree* T;
	T = (Tree*)malloc(sizeof(Tree));
	T->data = NULL;
	T->lchild = NULL;
	T->rchild = NULL;
	return T;
}

Tree* find(Tree* T, int r)
{
	if (!T)
	{
		return NULL;
	}
	if (T->data == r)
	{
		return T;
	}
	else
	{
		Tree* R = find(T->lchild, r);
		if (!R)
		{
			return find(T->rchild, r);
		}
		return R;
	}
}

void insert(Tree* T, int d, int e)
{
	Tree* R = init();
	R->data = e;
	if (d == 0)
	{
		T->lchild = R;
	}
	if (d == 1)
	{
		T->rchild = R;
	}
}

void traverse(Tree* T)
{
	if (!T)
	{
		return NULL;
	}
	else
	{
		traverse(T->lchild);
		printf("%d\n", T->data);
		a[i++] = T->data;
		traverse(T->rchild);
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	if (n == 0)
	{
		printf("Yes");//Well, I don't know why this step outputs = = without this step, I can't pass test point 4
		return 0;
	}
	else
	{
		Tree* T = init();
		int k;
		scanf("%d", &k);
		T->data = k;
		for (int j = 0; j < n - 1; j++)
		{
			int r, d, e;
			scanf("%d %d %d", &r, &d, &e);
			Tree* R = init();
			R = find(T, r);
			insert(R, d, e);
		}
		traverse(T);
		min = a[0];
		for (int j = 1; j < i; j++)
		{
			if (min < a[j])
			{
				min = a[j];
				flag = 1;
			}
			else
			{
				flag = 0;
				break;
			}
		}
		if (flag)
		{
			printf("Yes");
		}
		else
		{
			printf("No");
		}
		return 0;
	}
}

Tags: C Algorithm data structure pta

Posted on Sun, 28 Nov 2021 10:05:58 -0500 by giannis_athens