<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    小明思考

    Just a software engineer
    posts - 124, comments - 36, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    TopCoder Prerequisites解法

    Posted on 2011-10-25 13:28 小明 閱讀(1824) 評論(3)  編輯  收藏 所屬分類: 數據結構和算法
    原題:
    http://community.topcoder.com/stat?c=problem_statement&pm=164&rd=50

    Class Name: Prerequisites

    Mathod Name: orderClasses

    Parameters: String[]

    Returns: String[]

     

    You are a student at a college with the most unbelievably complex prerequisite

    structure ever. To help you schedule your classes, you decided to put together

    a program that returns the order in which the classes should be taken. 

     

    Implement a class Prerequisites which contains a method orderClasses. The

    method takes a String[] that contains the classes you must take and returns a

    String[] of classes in the order the classes should be taken so that all

    prerequisites are met.

     

    String[] elements will be of the form (and TopCoder will ensure this):

    "CLASS: PRE1 PRE2 ..." where PRE1 and PRE2 are prerequisites of CLASS. CLASS,

    PRE1, PRE2, ... consist of a department name (3 or 4 capital letters, A-Z

    inclusive) followed by a class number (an integer between 100 and 999,

    inclusive). The department name should be immediately followed by the class

    number with no additional characters, numbers or spaces (i.e. MATH217). It is

    not necessary for a class to have prerequisites. In such a case, the colon is

    the last character in the String. 

     

    You can only take one class at a time, therefore, use the following rules to

    determine which class to take :

    1) Any prerequisite class(es) listed for a class must be taken before the class

    can be taken.

    2) If multiple classes can be taken at the same time, you take the one with the

    lowest number first, regardless of department.

    3) If multiple classes with the same number can be taken at the same time, you

    take the department name which comes first in alphabetical order. 

    4) If the inputted course schedule has errors, return a String[] of length 0.

    There is an error if it is impossible to return the classes in an order such

    that all prerequisites are met, or if a prerequisite is a course that does not

    have its own entry in the inputted String[].

     

    Examples of valid input Strings are:

    "CSE111: CSE110 MATH101"

    "CSE110:"

     

    Examples of invalid input Strings are:

    "CS117:" (department name must consist of 3 - 4 capital letters, inclusive)

    "cs117:" (department name must consist of 3 - 4 capital letters, inclusive)

    "CS9E11:" (department name must be letters only)

    "CSE110: " (no trailing spaces allowed)

    "CSE110: CSE101 " (no trailing spaces allowed)

    "MATH211: MAM2222" (class number to large)

    "MATH211: MAM22" (class number to small)

    "ENGIN517: MATH211" (department name to large)

     

    Here is the method signature (be sure your method is public):

    String[] orderClasses(String[] classSchedule);

     

    TopCoder will make sure classSchedule contains between 1 and 20 Strings,

    inclusive, all of the form above. The Strings will have between 1 and 50

    characters, inclusive. TopCoder will check that the syntax of the Strings are

    correct: The Strings will contain a valid class name, followed by a colon,

    possibly followed by a series of unique prerequisite classes separated by

    single spaces. Also, TopCoder will ensure that each class has at most one

    entry in the String[].

     

    Examples:

    If classSchedule={

    "CSE121: CSE110",

    "CSE110:",

    "MATH122:",

    }

    The method should return: {"CSE110","CSE121","MATH122"}

     

    If classSchedule={

    "ENGL111: ENGL110",

    "ENGL110: ENGL111"

    }

    The method should return: {}

     

    If classSchedule=[

    "ENGL111: ENGL110"

    }

    The method should return: {}

     

    If classSchedule={

    "CSE258: CSE244 CSE243 INTR100"

    "CSE221: CSE254 INTR100"

    "CSE254: CSE111 MATH210 INTR100"

    "CSE244: CSE243 MATH210 INTR100"

    "MATH210: INTR100"

    "CSE101: INTR100"

    "CSE111: INTR100"

    "ECE201: CSE111 INTR100"

    "ECE111: INTR100"

    "CSE243: CSE254"

    "INTR100:"

    }

    The method should return:

    {"INTR100","CSE101","CSE111","ECE111","ECE201","MATH210","CSE254","CSE221","CSE2

    43","CSE244","CSE258"}

     

     

    Definition

              

    Class:      Prerequisites

    Method:   orderClasses

    Parameters:     String[]

    Returns: String[]

    Method signature:   String[] orderClasses(String[] param0)

    (be sure your method is public)


    ------------------------------------------------------------我的解法如下----------------------------------------

    想法很簡單,這道題本質上是一道排序問題,我們只需要定義好排序的邏輯,然后應用快排就可以了。


    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class Prerequisites {
        
    private static final String[] EMPTY = {};
        Map
    <String,Klass> classes = new HashMap<String,Klass>();
        
        
    static class Klass implements Comparable<Klass>{
            String name;
            
    int number;
            String dept;
            List
    <Klass> pres = new ArrayList<Klass>();
            
    boolean exist = false;
            
            Klass(String name){
                
    this.name = name;
                
    int len = name.length();
                
    this.number = Integer.parseInt(name.substring(len-3));
                
    this.dept =name.substring(0,len-3);
            }
            
            
    private boolean isPre(Klass k){
                
    if(k==thisreturn false;
                
                
    for(Klass p:this.pres){
                    
    if(k == p || p.isPre(k)) return true;
                }
                
    return false;
            }

            @Override
            
    public int compareTo(Klass that) {
                
    boolean pre = this.isPre(that);
                
    boolean sub = that.isPre(this);
                
                
    if(pre && sub){
                    
    throw new RuntimeException("circle detected");
                }
                
    else if(pre){
                    
    return 1;
                }
                
    else if(sub){
                    
    return -1;
                }
                
    if(this.number!=that.number) return this.number-that.number;
                
    return this.dept.compareTo(that.dept);
            }
        }
        
        
    private Klass getClass(String name){
            Klass k 
    = classes.get(name);
            
    if(k==null){
                k 
    = new Klass(name);
                classes.put(name, k);
            }
            
    return k;
        }
        
        
    public String[] orderClasses(String[] classSchedule){
            classes.clear();
            //parse the input
            
    for(String s:classSchedule){
                
    int idx = s.indexOf(":");
                String name 
    = s.substring(0,idx);
                Klass k 
    = getClass(name);
                k.exist 
    = true;
                
    if(idx!=s.length()-1){
                    String[] pres 
    = s.substring(idx+1).split(" ");
                    
    for(String pre:pres){
                        
    if(pre.length()>0){
                            Klass p 
    = getClass(pre);
                            k.pres.add(p);
                        }
                    }
                }
            }
            
            Klass [] sortedClasses 
    =  (Klass[]) classes.values().toArray(new Klass[0]);
            
    for(Klass k:sortedClasses){
                
    if(!k.exist){
                    
    return EMPTY;
                }
            }
            
    try {
                Arrays.sort(sortedClasses);
            } 
    catch (Exception e) {
                
    return EMPTY;
            }
            String[] result 
    = new String[sortedClasses.length];
            
    int c = 0;
            
    for(Klass k:sortedClasses){
                result[c
    ++= k.name;
            }
            
    return result;
        }
    }



    評論

    # re: TopCoder Prerequisites解法  回復  更多評論   

    2011-10-26 08:43 by tb
    沒有注釋 看起來有點暈 呵呵

    # re: TopCoder Prerequisites解法[未登錄]  回復  更多評論   

    2012-06-13 08:46 by 小光
    學到很多,謝謝

    # re: TopCoder Prerequisites解法[未登錄]  回復  更多評論   

    2013-04-16 19:00 by Harry
    scala version: for fun

    object Prerequest {
    case class Klass(dep: String, num: Int)
    def sort(a: Klass, b: Klass) = {
    if (a.num < b.num) true
    else if (a.dep < b.dep) true
    else false
    }
    def orderClasses(m: Map[Klass, List[Klass]]): List[Klass] = {
    def findPre(l: List[Klass]): List[Klass] = {
    l match {
    case Nil => Nil
    case s =>
    val u = for (c <- s) yield {
    val pre = m(c)
    findPre(pre) ::: List(c)
    }
    u.flatten
    }
    }
    try {
    val keys = m.keys.toList.sortWith((a, b) => sort(a, b))
    val s = keys.map {
    case k =>
    val sorted = m(k).sortWith((a, b) => sort(a, b))
    findPre(sorted) ::: List(k)
    }
    s.flatten.toList.distinct
    } catch {
    case e: Throwable => Nil
    }
    }
    def main(args: Array[String]): Unit = {
    val CSE258 = Klass("CSE", 258)
    val CSE244 = Klass("CSE", 244)
    val CSE243 = Klass("CSE", 243)
    val INTR100 = Klass("INTR", 100)
    val CSE221 = Klass("CSE", 221)
    val CSE254 = Klass("CSE", 254)
    val CSE111 = Klass("CSE", 111)
    val MATH210 = Klass("MATH", 210)
    val CSE101 = Klass("CSE", 101)
    val ECE201 = Klass("ECE", 201)
    val ECE111 = Klass("ECE", 111)

    val p1 =
    Map(CSE258 -> List(CSE244, CSE243, INTR100)) ++
    Map(CSE221 -> List(CSE254, INTR100)) ++
    Map(CSE254 -> List(CSE111, MATH210, INTR100)) ++
    Map(CSE244 -> List(CSE243, MATH210, INTR100)) ++
    Map(MATH210 -> List(INTR100)) ++
    Map(CSE101 -> List(INTR100)) ++
    Map(CSE111 -> List(INTR100)) ++
    Map(ECE201 -> List(CSE111, INTR100)) ++
    Map(ECE111 -> List(INTR100)) ++
    Map(CSE243 -> List(CSE254)) ++
    Map(INTR100 -> Nil)
    println(orderClasses(p1))
    }
    }

    主站蜘蛛池模板: 久久久久久久久免费看无码| 999久久久免费精品播放| 国拍在线精品视频免费观看| 亚洲视频.com| 国产福利视精品永久免费| 亚洲一区二区三区电影| 99视频精品全部免费观看| 亚洲综合一区二区| 黄色成人免费网站| 亚洲成AV人片高潮喷水| 国产片免费在线观看| 日韩精品无码免费视频| 亚洲精品国精品久久99热| 成年女人A毛片免费视频| 国产偷v国产偷v亚洲高清| 亚洲欧洲免费视频| 亚洲国产日韩女人aaaaaa毛片在线 | 夫妻免费无码V看片| 亚洲av最新在线观看网址| 免费a级黄色毛片| 羞羞视频免费网站在线看| 亚洲AV日韩AV天堂一区二区三区| 无码国产精品一区二区免费模式| 亚洲欧洲日产专区| 国产成人一区二区三区免费视频| 免费无码婬片aaa直播表情| 国产亚洲一区二区手机在线观看| 久久国产乱子伦精品免费看| 亚洲一级免费毛片| 在线永久免费观看黄网站| 久久久精品视频免费观看 | 1024免费福利永久观看网站| 色综合久久精品亚洲国产| 国产福利电影一区二区三区,亚洲国模精品一区 | 国产在线观看免费av站| 亚洲综合一区二区精品久久| 国产无遮挡吃胸膜奶免费看视频| 中国精品一级毛片免费播放| 亚洲一区无码中文字幕乱码| 亚洲国产成人久久综合野外| 777爽死你无码免费看一二区|