题目
输入样例1:
1 3
dean elsie mildred
elsie mildred dean
输出样例1:
B11
0B?
0?B
样例1解释
在这个样例中,单独一份论文 elsie-mildred-dean 并不能提供足够的信息判断 Elsie 比 Mildred 资历更深或更浅。
然而,我们可以推断出 Dean 一定比这两名研究员资历更深,从而资历排序为 Elsie<Mildred<Dean 和 Mildred<Elsie<Dean 均是可能的。
输入样例2:
2 3
elsie mildred dean
elsie mildred dean
elsie dean mildred
输出样例2:
B00
1B0
11B
样例2解释
在这个样例中,唯一能与两篇论文相一致的资历排序为 Elsie<Mildred<Dean,这是因为基于第一个样例所提供的信息,第二篇论文可以帮助我们推断出 Mildred 比 Elsie 的资历更深。
题解
积分值,资历越深,分数越高
若发现两个名字是字典序排列,则后者等于前者的分数
使用map来映射积分
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main(){
int k, n;
cin>>k>>n;
vector<string> nm;
vector<vector<string> > arr;
map<string, int> mp;
for(int i = 0; i < n; i++){
string str;
cin>>str;
nm.push_back(str);
mp[str] = 0;
}
// 积分值,资历越深,分数越高
// 若发现两个名字是字典序排列,则后者等于前者的分数
for(int i = 0; i < k; i++){
vector<string> temp; // 记录每个位置的名字
vector<int> score; // 记录每个位置的得分
for(int j = 0; j < n; j++){
string str;
cin>>str;
temp.push_back(str);
if(j == 0) score.push_back(0);
else if(str > temp[j-1]){
mp[str] += score[j-1];
score.push_back(score[j-1]);
}
else{
mp[str] += j;
score.push_back(j);
}
}
}
// map<string, int>::iterator itr, itr2;
for(int i = 0; i < nm.size(); i++){
for(int j = 0; j < nm.size(); j++){
if(i == j) cout<<"B";
else{
if(mp[nm[i]] > mp[nm[j]]) cout<<1;
else if(mp[nm[i]] == mp[nm[j]]) cout<<"?";
else cout<<0;
}
}
cout<<endl;
}
return 0;
}