SOJ 1006

1006. Team Rankings

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets, the Cats, the Dribblers, and the Elephants. When Scoop McGee, sports editor of the paper, gets the rankings from the selected local experts down at the hardware store, he's dismayed to find that there doesn't appear to be total agreement and so he's wondering what ranking to publish that would most accurately reflect the rankings he got from the experts. He’s found that finding the median ranking from among all possible rankings is one way to go. 

The median ranking is computed as follows: Given any two rankings, for instance ACDBE and ABCDE, the distance between the two rankings is defined as the total number of pairs of teams that are given different relative orderings. In our example, the pair B, C is given a different ordering by the two rankings. (The first ranking has C above B while the second ranking has the opposite.) The only other pair that the two rankings disagree on is B, D; thus, the distance between these two rankings is 2. The median ranking of a set of rankings is that ranking whose sum of distances to all the given rankings is minimal. (Note we could have more than one median ranking.) The median ranking may or may not be one of the given rankings. 

Suppose there are 4 voters that have given the rankings: ABDCE, BACDE, ABCED and ACBDE. Consider two candidate median rankings ABCDE and CDEAB. The sum of distances from the ranking ABCDE to the four voted rankings is 1 + 1 + 1 + 1 = 4. We'll call this sum the value of the ranking ABCDE. The value of the ranking CDEAB is 7 + 7 + 7 + 5 = 26. 

It turns out that ABCDE is in fact the median ranking with a value of 4. 

Input

There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input.

Output

Output for each input set should be one line of the form: 

ranking is the median ranking with value value. 

Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically. 

Sample Input

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.



  比较简单,用暴力做法也就是把总共120种组合与给出排名的距离都算出来,距离最小的就是最佳中和排名。具体过程见代码注释。

// Problem#: 1006
// Submission#: 4921310
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

void ini(int dis[])//重置数组 
{
    for(int i=0;i<120;++i)
    dis[i]=0;
}

int main()
{
    int distance[120];// ABCDE全排列一共120种,distance数组保存了每种与给定排名的距离 
    string allper[120];//保存ABCDE的全排列 
    int n;
    string A="ABCDE";
    allper[0]=A;
    for(int i=1;i<120;++i)
    {
        next_permutation(A.begin(),A.end());//next_permutation函数可以得到一个比所输入排列要大(字典序)的最小排列,具体内容可百度,运用此函数可以快速得到ABCDE的全排列 
        allper[i]=A;
        
        
        
    }
   
    while(cin>>n&&n)
    {
        
        ini(distance);
        string onerank;
        for(int current=0;current<n;++current)
        {
            cin>>onerank;
          for(int i=0;i<120;++i)
          {
             for(int j=0;j<4;++j)
             {
               for(int k=j+1;k<5;++k)
               {
                  if(onerank.find(allper[i][j])>onerank.find(allper[i][k])) //ABCDE一共有24组相对关系,即A-B,A-C,A-D,A-E,B-C……若allper[i]中某组组合的相对位置与给出的排名相反,则距离增加1 
                  distance[i]++;
               }
             }
          }
        }
        int min=distance[0];
        int pos=0;
        for(int i=1;i<120;++i)//找出distance数组中的最小值,即最小距离 
        {
            if(min>distance[i])
            {
            min=distance[i];
            pos=i;
            }
        }
        cout<<allper[pos]<<" is the median ranking with value "<<distance[pos]<<'.'<<endl;
        
        
        
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值