[Ybtoj high-efficiency advanced 2.1] E. Same birthday [string]

Insert picture description here
Insert picture description here

analysis

Sort by birthday date first , and the second key is the length of the name .

Then directly consider the different birthday dates . If it is always the same month and the same day, output the name all the time, if it is not the same month and the same day, output the date in a new line and output the name.

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct lwx//My struct is a dog
{
    
    
	string name;
	int month,day;
	bool operator<(const lwx& a)const{
    
    
        if(month!=a.month) return month<a.month;
        if(day!=a.day) return day<a.day;  //排序
        if(name.length()!=a.name.length()) return name.length()<a.name.length();  
        return name<a.name;
    }
}a[100001];
int n,k;
int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i].name>>a[i].month>>a[i].day;
	}
	sort(a+1,a+n+1); 
	int nxm=a[1].month,nxd=a[1].day;
	for(int i=2;i<=n;i++)
	{
    
    
		if(nxm!=a[i].month||nxd!=a[i].day)
		{
    
    
			nxm=a[i].month;
			nxd=a[i].day; 
		}
		else k++;
	}
	if(k)
	{
    
    
		cout<<a[1].month<<' '<<a[1].day<<' ';
		cout<<a[1].name<<' ';
		nxm=a[1].month;
		nxd=a[1].day;
		for(int i=2;i<=n;i++)
		{
    
    
			if(a[i].month!=nxm||a[i].day!=nxd)
			{
    
    
				cout<<endl;
				cout<<a[i].month<<' '<<a[i].day<<' ';
				cout<<a[i].name<<' ';
				nxm=a[i].month;
				nxd=a[i].day;
			}
			else
			{
    
    
				cout<<a[i].name<<' ';
			}
		}
	}
	else
	{
    
    
		cout<<"None";
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/113356372