YbtOJ 字符串处理课堂过关 例5 生日相同【字符串】

题目

在这里插入图片描述

思路

这道题我们可以直接模拟寻找过程。
先把能合并的生日和名字合并,
然后再把重复人数大于等于二的提取出来,
最后关键字排序输出。

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
int n,w,zsl;
struct node
{
	string s;
	int sy,sr;
}a[10010];
struct node2
{
	string s2[100];
	int sy2,sr2,cc;
}a2[10010];
struct node3
{
	string s3[100];
	int sy3,sr3,ccc;
}a3[10010];
bool cmp(const node&x,const node&y)
{
	if(x.s.size()==y.s.size())
	  return x.s<y.s;
	else
	  return x.s.size()<y.s.size();
}
bool cmp2(const node3&x,const node3&y)
{
	if(x.sy3!=y.sy3)
	  return x.sy3<y.sy3;
	if(x.sy3==y.sy3)
	  return x.sr3<y.sr3;
}
int main()
{
	cin>>n;
	for(int i=1; i<=n; i++)
	 	cin>>a[i].s>>a[i].sy>>a[i].sr;
	sort(a+1,a+1+n,cmp);
	/*for(int i=1; i<=n; i++)
	   cout<<a[i].s<<" "<<a[i].sy<<" "<<a[i].sr<<endl;*/
	for(int i=1; i<=n; i++)               合并相同生日🎂
	 {
	 	w=0;
	    for(int j=1; j<=zsl; j++)
	     {
	     	if(a[i].sy==a2[j].sy2&&a[i].sr==a2[j].sr2)
	     	 {
	     	   a2[j].cc++,a2[j].s2[a2[j].cc]=a[i].s,w=1;
	     	   break;
			 }
		 }
		if(w==0)
		 {
		 	zsl++;
		 	a2[zsl].cc=1,a2[zsl].s2[1]=a[i].s;
		 	a2[zsl].sy2=a[i].sy,a2[zsl].sr2=a[i].sr;
		 }
	 }
	int c12=0;
//	for(int i=1; i<=zsl; i++)
//	   cout<<a2[i].cc<<endl;
	for(int i=1; i<=zsl; i++)   
	 if(a2[i].cc>=2)       提取大于等于二的人数🌧
	  {
	     a3[++c12].sr3=a2[i].sr2,a3[c12].sy3=a2[i].sy2;
	     a3[c12].ccc=a2[i].cc;
	  	 for(int j=1; j<=a2[i].cc; j++)
	  	    a3[c12].s3[j]=a2[i].s2[j];
	  	 //cout<<a3[c12].sr3<<" "<<a3[c12].sy3<<" "<<a3[c12].ccc<<endl;
	  }
	sort(a3+1,a3+1+c12,cmp2);     排序输出📖
	for(int i=1; i<=c12; i++)
	 {
	    cout<<a3[i].sy3<<" "<<a3[i].sr3<<" ";
	    for(int j=1; j<=a3[i].ccc; j++)
	       cout<<a3[i].s3[j]<<" ";
	    cout<<endl;
	 }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值