测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A25058. 清点代码库

填空题 困难

题目描述

清点代码库

题目描述

很久之前新浪微博有人发过:“阿里代码库有几亿行代码,但其中有很多功能重复的代码,比如单单快排就被重写了几百遍。请设计一个程序,能够将代码库中所有功能重复的代码找出。各位大佬有啥想法,我当时就懵了,然后就挂了。。。”

这里我们把问题简化一下:首先假设两个功能模块如果接受同样的输入,总是给出同样的输出,则它们就是功能重复的;其次我们把每个模块的输出都简化为一个整数(在 int 范围内)。于是我们可以设计一系列输入,检查所有功能模块的对应输出,从而查出功能重复的代码。你的任务就是设计并实现这个简化问题的解决方案。

输入

输入在第一行中给出 2 个正整数,依次为 N(≤ 104)和 M(≤ 102),对应功能模块的个数和系列测试输入的个数。 随后 N 行,每行给出一个功能模块的 M 个对应输出,数字间以空格分隔。

输出

首先在第一行输出不同功能的个数 K。随后 K 行,每行给出具有这个功能的模块的个数,以及这个功能的对应输出。数字间以 1 个空格分隔,行首尾不得有多余空格。输出首先按模块个数非递增顺序,如果有并列,则按输出序列的递增序给出。 注:所谓数列 { A1, …, AM } 比 { B1, …, BM } 大,是指存在 1 ≤ i < M,使得 A1=B1,…,Ai=Bi 成立,且 Ai+1 > Bi+1。

样例输入

7 3
35 28 74
-1 -1 22
28 74 35
-1 -1 22
11 66 0
35 28 74
35 28 74

样例输出

4
3 35 28 74
2 -1 -1 22
1 11 66 0
1 28 74 35

参考答案

#include<bits/stdc++.h> using namespace std; #define PII pair<int,int> const int INF = 0x3f3f3f3f; const int N = 1e4+10; struct cmp{ //自定义set排序 bool operator() (const pair<int,vector<int> >&a, const pair<int,vector<int> >&b) const{ if(a.first!=b.first) return a.first>b.first; else return a.second<b.second; } }; int main(){ int n, m; scanf("%d%d", &n, &m); set<vector<int> > st; //存模块 map<vector<int>, int> mp; //存每个模块的个数 set<pair<int,vector<int> >,cmp > St;//排序 for ( int i = 0 ; i < n ; i ++ ){ vector<int> v; for ( int j = 0 ; j < m ; j ++ ){ int x; scanf("%d", &x); v.push_back(x); } mp[v] ++; st.insert(v); } printf("%d\n", st.size()); //把所有模块存入ST排序 set<vector<int> >::iterator it; for(it = st.begin() ; it != st.end() ; it ++) St.insert({mp[*it],*it}); //输出ST set<pair<int,vector<int> > >::iterator ite; for(ite = St.begin() ; ite != St.end() ; ite ++){ cout << (*ite).first; for(int i = 0; i < (*ite).second.size() ; i++) cout<<' '<<(*ite).second[i]; cout<<endl; } return 0; }
上一题 下一题