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

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

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

    emu in blogjava

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks


    Problem Statement
    ????
    You have a collection of music files with names formatted as "genre-artist-album-song" (quotes for clarity only), where genre, artist, album, and song each consist of only lowercase letters ('a'-'z') and spaces (but no leading/trailing spaces). The collection is given in the String[] collection. You would like to filter the songs according to a set of criteria given in the String[] filterInfo. Each element of filterInfo is an equality check formatted as "field=value" (quotes for clarity only), where field is "genre", "artist", "album", or "song", and value consists of only lowercase letters ('a'-'z') and spaces (but no leading/trailing spaces). For a file to pass through the filter, it must satisfy every equality check in filterInfo. For example, if filterInfo = {"genre=country", "album=greatest hits"}, only songs from country greatest hits albums should be returned. Return a String[] containing all the files that meet the given criteria in the same relative order as they appear in collection.
    Definition
    ????
    Class:
    SongFilter
    Method:
    filter
    Parameters:
    String[], String[]
    Returns:
    String[]
    Method signature:
    String[] filter(String[] collection, String[] filterInfo)
    (be sure your method is public)
    ????

    Constraints
    -
    collection will contain between 1 and 50 elements, inclusive.
    -
    Each element of collection will be formatted as described in the problem statement.
    -
    Each element of collection will contain between 7 and 50 characters, inclusive.
    -
    Each genre, artist, album, and song in collection will contain between 1 and 20 characters, inclusive.
    -
    collection will contain no duplicate elements.
    -
    filterInfo will contain between 1 and 4 elements, inclusive.
    -
    Each element of filterInfo will be formatted as described in the problem statement.
    -
    Each value in filterInfo will contain between 1 and 20 characters, inclusive.
    Examples
    0)

    ????
    {"jazz-joe pass-virtuoso-cherokee",
     "rock-led zeppelin-ii-lemon song",
     "country-dwight yoakam-long way home-things change",
     "metal-iron maiden-powerslave-aces high",
     "pop-supremes-more hits-ask any girl",
     "rock-faith no more-angel dust-rv",
     "jazz-chuck mangione-feels so good-feels so good",
     "rock-van halen-ii-spanish fly"}
    {"genre=rock", "album=ii"}
    Returns: {"rock-led zeppelin-ii-lemon song", "rock-van halen-ii-spanish fly" }
    This filter returns all the rock songs from albums with the title "ii".
    1)

    ????
    {"rock-jimi hendrix-axis bold as love-little wing",
     "rock-cars-cars-moving in stereo",
     "rock-jimi hendrix-electric ladyland-gypsy eyes",
     "blues-albert collins-ice pickin-ice pick",
     "rock-jimi hendrix-axis bold as love-bold as love",
     "rock-jimi  hendrix-axis bold as love-exp"}
    {"artist=jimi hendrix", "album=axis bold as love"}
    Returns:
    {"rock-jimi hendrix-axis bold as love-little wing",
     "rock-jimi hendrix-axis bold as love-bold as love" }
    This filter returns all the songs that are from the album "axis bold as love" by the artist "jimi hendrix". The last element in the collection is not returned because there are two spaces between "jimi" and "hendrix".
    2)

    ????
    {"rock-ozzy osbourne-blizzard of ozz-dee",
     "rock-marvelous three-hey album-let me go",
     "rock-cheap trick-in color-downed"}
    {"genre=soul"}
    Returns: { }
    There is no soul music in this collection, so an empty String[] is returned.
    3)

    ????
    {"country-topcoder-the country album-twangy",
     "rock-topcoder-the rock album-rockin",
     "jazz-topcoder-the jazz album-jazzy",
     "soul-topcoder-the soul album-soulful",
     "metal-topcoder-the metal album-thrash"}
    {"artist=topcoder", "genre=jazz", "album=the jazz album", "song=jazzy"}
    Returns: {"jazz-topcoder-the jazz album-jazzy" }

    4)

    ????
    {"pop-jackson five-abc-the love you save",
     "rock-ac dc-powerage-riff raff"}
    {"genre=pop", "genre=rock"}
    Returns: { }
    No single element of collection can represent more than one genre, so this filter returns an empty String[].
    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

    posted on 2005-08-23 11:47 emu 閱讀(1332) 評論(9)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

    # emu的答案 2005-08-23 11:48 emu
    這也太容易了吧?


    import java.util.*;

    public class SongFilter {
        public static void main(String[] args) {
            String[] collection, filterInfo, result;
            SongFilter sf = new SongFilter();
            collection = new String[] {"jazz-joe pass-virtuoso-cherokee",
                         "rock-led zeppelin-ii-lemon song",
                         "country-dwight yoakam-long way home-things change",
                         "metal-iron maiden-powerslave-aces high",
                         "pop-supremes-more hits-ask any girl",
                         "rock-faith no more-angel dust-rv",
                         "jazz-chuck mangione-feels so good-feels so good",
                         "rock-van halen-ii-spanish fly"};
            filterInfo = new String[] {"genre=rock", "album=ii"};
            result = sf.filter(collection, filterInfo);
            for (int i = 0; i < result.length; i++)
                System.out.println(result[i]);
        }

        String[] filterPrefix = new String[] {"genre=", "artist=", "album=",
                                "song="};
        int filterPrefixLength = filterPrefix.length;
        public String[] filter(String[] collection, String[] filterInfo) {
            String[] filter = new String[filterPrefixLength];
            for (int i = 0; i < filterInfo.length; i++)
                for (int j = 0; j < filterPrefixLength; j++)
                    if (filterInfo[i].startsWith(filterPrefix[j]))
                        if (filter[j] == null)
                            filter[j] = filterInfo[i].substring(filterPrefix[j].
                                    length());
                        else if (!filter[j].equals(filterInfo[i].substring(
                                filterPrefix[j].length())))
                            return new String[0];
            ArrayList tmpResult = new ArrayList();
            for (int i = 0; i < collection.length; i++) {
                String[] collectionDetail = collection[i].split("-"); //genre-artist-album-song
                boolean match = true;
                for (int j = 0; j < filterPrefixLength; j++)
                    if (filter[j] != null &&
                        !collectionDetail[j].equals(filter[j]))
                        match = false;
                if (match)
                    tmpResult.add(collection[i]);
            }
            String[] result = new String[tmpResult.size()];
            tmpResult.toArray(result);
            return result;
        }
    }

      回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-08 17:37 drekar
    我用正則表達式做的:

    import java.util.ArrayList;
    import java.util.regex.Pattern;

    public class SongFilter {

    public String[] filter(String[] collection, String[] filterInfo) {
    if (null == collection || null == filterInfo || filterInfo.length > 4)
    return null;

    // build filter pattern
    String [] filters = {"([a-z\\s]+)", "([a-z\\s]+)", "([a-z\\s]+)", "([a-z\\s]+)"};
    for (int i=0; i<filterInfo.length; i++) {
    String[] temp = filterInfo[i].split("=");

    if (temp[0].equals("genre")) filters[0] = temp[1];
    else if (temp[0].equals("artist")) filters[1] = temp[1];
    else if (temp[0].equals("album")) filters[2] = temp[1];
    else /* "song" */ filters[3] = temp[1];
    }
    String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];

    ArrayList tmpResult = new ArrayList();
    Pattern p = Pattern.compile(filterPattern);
    for (int i=0; i<collection.length; i++)
    if (p.matcher(collection[i]).matches())
    tmpResult.add(collection[i]);

    String[] result = new String[tmpResult.size()];
    tmpResult.toArray(result);
    return result;
    }

    /**
    * 程序主入口
    *
    * @param args
    */
    public static void main(String[] args) {
    SongFilter sf = new SongFilter();
    String [] myCollection = {"jazz-joe pass-virtuoso-cherokee",
    "rock-led zeppelin-ii-lemon song",
    "country-dwight yoakam-long way home-things change",
    "metal-iron maiden-powerslave-aces high",
    "pop-supremes-more hits-ask any girl",
    "rock-faith no more-angel dust-rv",
    "jazz-chuck mangione-feels so good-feels so good",
    "rock-van halen-ii-spanish fly"};
    String [] myFilter = {"genre=rock", "album=ii"};

    String [] result = sf.filter(myCollection, myFilter);
    for (int i = 0; i < result.length; i++)
    System.out.println(result[i]);

    }
    }  回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-08 17:47 drekar
    重新排了一下版:

    import java.util.ArrayList;
    import java.util.regex.Pattern;

    public class SongFilter {

     public String[] filter(String[] collection, String[] filterInfo) {
      if (null == collection || null == filterInfo || filterInfo.length > 4)
       return null;
      
      // build filter pattern
      String [] filters = {"([a-z\\s]+)", "([a-z\\s]+)", "([a-z\\s]+)", "([a-z\\s]+)"};
      for (int i=0; i<filterInfo.length; i++) {
       String[] temp = filterInfo[i].split("=");

       if (temp[0].equals("genre"))   filters[0] = temp[1];
       else if (temp[0].equals("artist")) filters[1] = temp[1];
       else if (temp[0].equals("album")) filters[2] = temp[1];
       else       /* "song" */  filters[3] = temp[1];
      }
      String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];

      ArrayList tmpResult = new ArrayList();
      Pattern p = Pattern.compile(filterPattern);
      for (int i=0; i<collection.length; i++)
       if (p.matcher(collection[i]).matches())
        tmpResult.add(collection[i]);
      
        String[] result = new String[tmpResult.size()];
        tmpResult.toArray(result);
        return result;
     }

     /**
      * 程序主入口
      *
      * @param args
      */
     public static void main(String[] args) {
      SongFilter sf = new SongFilter();
      String [] myCollection = {"jazz-joe pass-virtuoso-cherokee",
         "rock-led zeppelin-ii-lemon song",
         "country-dwight yoakam-long way home-things change",
         "metal-iron maiden-powerslave-aces high",
         "pop-supremes-more hits-ask any girl",
         "rock-faith no more-angel dust-rv",
         "jazz-chuck mangione-feels so good-feels so good",
         "rock-van halen-ii-spanish fly"};
      String [] myFilter = {"genre=rock", "album=ii"};
      
      String [] result = sf.filter(myCollection, myFilter);
        for (int i = 0; i < result.length; i++)
          System.out.println(result[i]);
     }
    }  回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-08 20:55 emu
    不錯。開始編譯通不過嚇了我一跳,原來你用中文空格替換掉制表符了。

    用正則做要比我的做法好一點,需要匹配復雜一點的規(guī)則的時候很容易改。

    我不用正則是因為之前從來沒有在java里面用過正則,當時趕時間什么熟悉用什么,不可能臨時抱佛腳去查手冊。

    此外題目中沒有明確,filter的條件能否重復定義,如果重復了是按照“與”邏輯還是“或”邏輯處理。(“For a file to pass through the filter, it must satisfy every equality check in filterInfo”暗示了這種情況下也應(yīng)該按照與邏輯)所以在這組數(shù)據(jù)下:
    collection = new String[] {"jazz-joe pass-virtuoso-cherokee",
    "rock-led zeppelin-ii-lemon song",
    "country-dwight yoakam-long way home-things change",
    "metal-iron maiden-powerslave-aces high",
    "pop-supremes-more hits-ask any girl",
    "rock-faith no more-angel dust-rv",
    "jazz-chuck mangione-feels so good-feels so good",
    "rock-van halen-ii-spanish fly"};

    filterInfo = new String[] {"genre=rock", "album=ii", "album=angel dust"};

    我的答案是空數(shù)組(按照與邏輯),而你的卻返回
    rock-faith no more-angel dust-rv,匹配最后出現(xiàn)的相同條件。


    想想,用正則的話這個與或邏輯如何寫?

      回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-09 09:56 drekar
    呵呵,不知道你是怎么排版的,只好用中文空格了。

    我確實沒想到filter出現(xiàn)重復定義的情況,下面是修改后的代碼。

    (1)如果是邏輯“或”的話filter改為

     public String[] filter(String[] collection, String[] filterInfo) {
      if (null == collection || null == filterInfo)
       return new String[0];
      
      // build filter pattern
      String[] filters = { "", "", "", "" };
      for (int i = 0; i < filterInfo.length; i++) {
       String[] temp = filterInfo[i].split("=");

       if (temp[0].equals("genre"))    filters[0] += "|(" + temp[1] + ")";
       else if (temp[0].equals("artist"))  filters[1] += "|(" + temp[1] + ")";
       else if (temp[0].equals("album"))  filters[2] += "|(" + temp[1] + ")";
       else      /* "song" */    filters[3] += "|(" + temp[1] + ")";
      }
      
      for (int i = 0; i < 4; i++) {
       if (filters[i].equals(""))
        filters[i] = "([a-z\\s]+)";
       else
        filters[i] = "(" + filters[i].substring(1) + ")";
      }

      String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];
      
      ArrayList tmpResult = new ArrayList();
      Pattern p = Pattern.compile(filterPattern);
      for (int i=0; i<collection.length; i++)
       if (p.matcher(collection[i]).matches())
        tmpResult.add(collection[i]);
      
        String[] result = new String[tmpResult.size()];
        tmpResult.toArray(result);
        return result;
     }

    (2)如果是邏輯“與”的話filter改為
     public String[] filter(String[] collection, String[] filterInfo) {
      if (null == collection || null == filterInfo)
       return new String[0];
      
      // build filter pattern
      String[] filters = { "", "", "", "" };
      for (int i = 0; i < filterInfo.length; i++) {
       String[] temp = filterInfo[i].split("=");

       if (temp[0].equals("genre"))    filters[0] += "_" + temp[1];
       else if (temp[0].equals("artist"))  filters[1] += "_" + temp[1];
       else if (temp[0].equals("album"))  filters[2] += "_" + temp[1];
       else      /* "song" */    filters[3] += "_" + temp[1];
      }
      
      for (int i = 0; i < 4; i++) {
       if (filters[i].equals(""))
        filters[i] = "([a-z\\s]+)";
       else
        filters[i] = filters[i].substring(1);
      }

      String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];
      
      ArrayList tmpResult = new ArrayList();
      Pattern p = Pattern.compile(filterPattern);
      for (int i=0; i<collection.length; i++)
       if (p.matcher(collection[i]).matches())
        tmpResult.add(collection[i]);
      
        String[] result = new String[tmpResult.size()];
        tmpResult.toArray(result);
        return result;
     }  回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-09 10:05 drekar
    進一步,在上面的"與"邏輯代碼里,如果正則表達式里出現(xiàn)了"_"字符,說明無需進行后面的匹配,直接返回結(jié)果即可。

    下面是修改的filter代碼:
    (3) 邏輯"與" (解二)

     final static char invalidChar = '_';
     
     public String[] filter(String[] collection, String[] filterInfo) {
      if (null == collection || null == filterInfo)
       return new String[0];
      
      // build filter pattern
      String[] filters = { "", "", "", "" };
      for (int i = 0; i < filterInfo.length; i++) {
       String[] temp = filterInfo[i].split("=");

       if (temp[0].equals("genre"))   filters[0] += invalidChar + temp[1];
       else if (temp[0].equals("artist")) filters[1] += invalidChar + temp[1];
       else if (temp[0].equals("album")) filters[2] += invalidChar + temp[1];
       else      /* "song" */    filters[3] += invalidChar + temp[1];
      }
      
      for (int i = 0; i < 4; i++) {
       if (filters[i].equals(""))
        filters[i] = "([a-z\\s]+)";
       else
        filters[i] = filters[i].substring(1);
      }

      String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];
      if (filterPattern.indexOf(invalidChar) >= 0)
       return new String[0];
      
      ArrayList tmpResult = new ArrayList();
      Pattern p = Pattern.compile(filterPattern);
      for (int i=0; i<collection.length; i++)
       if (p.matcher(collection[i]).matches())
        tmpResult.add(collection[i]);
      
        String[] result = new String[tmpResult.size()];
        tmpResult.toArray(result);
        return result;
     }  回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-09 10:53 emu
    呵呵,250分的小題目也要小心在意啊。上次我記得入圍的全部都是三道全對的。

    測了一下,在這個條件下:
    String [] myFilter = {"genre=rock", "album=ii", "album=ii"};

    你的與邏輯居然返回空!你寫完程序不測功能的嗎?
      回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-09 13:25 drekar
    見教的是。慚愧啊。
    又改了一下“與邏輯”:

     final static String WildCardString = "([a-z\\s]+)";
     
     public String[] filter(String[] collection, String[] filterInfo) {
      if (null == collection || null == filterInfo)
       return new String[0];
      
      // build filter pattern
      String[] filterNames = {"genre", "artist", "album", "song"};
      String[] filters = { WildCardString, WildCardString, WildCardString, WildCardString };

      for (int i = 0; i < filterInfo.length; i++) {
       String[] temp = filterInfo[i].split("=");

       for (int j = 0; j < 4; j++)
        if (temp[0].equals(filterNames[j])) {
         if (filters[j].equals(WildCardString))
          filters[j] = temp[1]; // set filter
         else if (!filters[j].equals(temp[1]))
          return new String[0]; // conflicting filters
         break;
        }
      }
      
      String filterPattern = filters[0] + "-" + filters[1] + "-" + filters[2] + "-" + filters[3];
      
      ArrayList tmpResult = new ArrayList();
      Pattern p = Pattern.compile(filterPattern);
      for (int i=0; i<collection.length; i++)
       if (p.matcher(collection[i]).matches())
        tmpResult.add(collection[i]);
      
        String[] result = new String[tmpResult.size()];
        tmpResult.toArray(result);
        return result;
     }  回復  更多評論
      

    # re: SongFilter (入圍賽250真題) 2005-12-09 14:47 emu
    else if (!filters[j].equals(temp[1]))
    return new String[0]; // conflicting filters

    跟我的

    else if (!filter[j].equals(filterInfo[i].substring(filterPrefix[j].length())))
    return new String[0];

    同出一轍呵呵  回復  更多評論
      

    主站蜘蛛池模板: 性无码免费一区二区三区在线| 久久狠狠躁免费观看2020| 亚洲性猛交XXXX| 99热在线精品免费播放6| 亚洲欧美国产日韩av野草社区| 无码欧精品亚洲日韩一区夜夜嗨 | 久久免费精品一区二区| 国产精品无码免费专区午夜| 久久久久无码精品亚洲日韩| 精品久久久久国产免费| 51午夜精品免费视频| 亚洲一级毛片在线播放| 国产午夜成人免费看片无遮挡| 亚洲最大黄色网址| 亚洲午夜精品久久久久久浪潮| 久久精品国产免费观看三人同眠| 色屁屁www影院免费观看视频| 久久久久亚洲精品日久生情 | 亚洲色成人中文字幕网站| 日韩欧美一区二区三区免费观看 | 中国一级特黄的片子免费 | 又大又硬又粗又黄的视频免费看| 亚洲明星合成图综合区在线| 亚洲中文字幕视频国产| 成人一a毛片免费视频| 久久精品国产免费| 无码毛片一区二区三区视频免费播放 | 免费在线精品视频| 成人女人A级毛片免费软件| 十八禁视频在线观看免费无码无遮挡骂过 | 国产精品免费观看| 国产无遮挡裸体免费视频在线观看| 亚洲av无码成人精品区一本二本| 亚洲综合一区二区精品久久| 中文字幕精品亚洲无线码一区| 日本人的色道www免费一区| 99爱在线精品免费观看| 久久免费福利视频| 三年片免费观看大全国语| 男女污污污超污视频免费在线看| 亚洲AV无码一区二区三区牛牛|