[Ybt Advanced 2-1-5] Same birthday

Same birthday

Topic link: ybt efficient advanced 2-1-5

Topic

There are a bunch of people who give their birthdays and ask you to find all people with the same birthday.
Dates are sorted from first to last, with names from short to long, and then lexicographically.

Ideas

This question is actually a simple simulation.

What I use is to open a vector, and each record the birthday of a person with a number on a certain day.

Then enumerate the days from the beginning to the end. If there is more than one person, sort them according to the requirements of the title and output.

The problem is solved with string, and the information of all people is sorted directly by date and name.
Then directly sweep over, and output the end of the same length over one for each consecutive day.

Code

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

using namespace std;

int n, x, y, size[100001], tmp[100001];
char name[100001][256];
vector <int> same[13][32];//记录同一天生日的有哪些人
bool write;

bool cmp(int x, int y) {
    
    
	if (size[x] != size[y]) return size[x] < size[y];//先按长短排
	for (int i = 0; i < size[x]; i++)//长短一样,按字典序排
		if (name[x][i] != name[y][i])
			return name[x][i] < name[y][i];
}

int main() {
    
    
	scanf("%d", &n);
	
	for (int i = 1; i <= n; i++) {
    
    
		cin >> name[i];
		size[i] = strlen(name[i]);
		scanf("%d %d", &x, &y);
		same[x][y].push_back(i);
	}
	
	for (int i = 1; i <= 12; i++)
		for (int j = 1; j <= 31; j++) {
    
    
			x = same[i][j].size();
			if (x <= 1) continue;//只有一个人或者没有人
			
			write = 1;//有人在同一天生日,记录一下
			
			for (int k = 0; k < x; k++)
				tmp[k] = same[i][j][k];
			sort(tmp, tmp + x, cmp);//排序
			
			printf("%d %d ", i, j);
			for (int k = 0; k < x; k++)
				printf("%s ", name[tmp[k]]);
			cout << endl;//不知道为什么,用 printf("\n") 和 puts("") 都没有用,要用 cout << endl
		}
	
	if (!write) printf("None");//没有人在同一天生日
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/112853233