poj 1007 北大ACM java

  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import java.util.Scanner;  
  6.   
  7. class Main {  
  8.   
  9.     static class Node implements Comparable{  
  10.   
  11.         String str="";  
  12.         int id=0;  
  13.         int sortN=0;  
  14.           
  15.         Node(String str,int id,int sortN){  
  16.             this.str=str;  
  17.             this.id=id;  
  18.             this.sortN=sortN;  
  19.         }  
  20.           
  21.         public int compareTo(Object o) {  
  22.             Node node=(Node)o;  
  23.             if(node.sortN==sortN){  
  24.                 return node.id<id?1:(node.id==id?0:-1);  
  25.             }else{  
  26.                 return node.sortN<sortN?1:-1;  
  27.             }  
  28.         }  
  29.           
  30.     }  
  31.       
  32.     private static int getSortN(String str){  
  33.         int len=str.length();  
  34.         int ans=0;  
  35.         for(int i=0;i<len;i++){  
  36.             for(int j=i+1;j<len;j++){  
  37.                 if(str.charAt(i)>str.charAt(j))  
  38.                     ans++;  
  39.             }  
  40.         }  
  41.         return ans;  
  42.     }  
  43.       
  44.     public static void main(String[] args) {  
  45.         Scanner in = new Scanner(System.in);  
  46.         in.nextInt();  
  47.         int ncase=in.nextInt();  
  48.         List<Node> l=new ArrayList<Node>();  
  49.         for(int i=0;i<ncase;i++){  
  50.             String str=in.next();  
  51.             l.add(new Node(str,i,getSortN(str)));  
  52.         }  
  53.         Collections.sort(l);  
  54.         for(Iterator<Node> it=l.iterator();it.hasNext();){  
  55.             System.out.println(it.next().str);  
  56.         }  
  57.     }  
  58. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值