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

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

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

    emu in blogjava

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      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 閱讀(1322) 評論(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
    不錯。開始編譯通不過嚇了我一跳,原來你用中文空格替換掉制表符了。

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

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

    此外題目中沒有明確,filter的條件能否重復定義,如果重復了是按照“與”邏輯還是“或”邏輯處理。(“For a file to pass through the filter, it must satisfy every equality check in filterInfo”暗示了這種情況下也應該按照與邏輯)所以在這組數據下:
    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"};

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


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

      回復  更多評論
      

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

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

    (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
    進一步,在上面的"與"邏輯代碼里,如果正則表達式里出現了"_"字符,說明無需進行后面的匹配,直接返回結果即可。

    下面是修改的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];

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

    主站蜘蛛池模板: 亚洲Av无码乱码在线播放| 亚洲成a∨人片在无码2023| 国产人妖ts在线观看免费视频| 亚洲精品第五页中文字幕| 全免费A级毛片免费看网站| 亚洲人成在久久综合网站| 免费观看激色视频网站bd | 美女露隐私全部免费直播| 最近2019中文免费字幕| 亚洲精品av无码喷奶水糖心| 成年人性生活免费视频| 色天使色婷婷在线影院亚洲| 亚洲M码 欧洲S码SSS222| 亚美影视免费在线观看| 亚洲国产精品不卡在线电影| 国产92成人精品视频免费| 亚洲人成高清在线播放| 在线免费观看色片| 一级特黄录像视频免费| 亚洲国产精品无码久久一线| 99精品视频在线免费观看| 中文文字幕文字幕亚洲色| 日韩一区二区免费视频| 九一在线完整视频免费观看| 人人狠狠综合久久亚洲婷婷| 国产免费不卡视频| 亚洲午夜精品久久久久久人妖| 2015日韩永久免费视频播放| 亚洲精品无码av人在线观看 | igao激情在线视频免费| 久久久久亚洲av无码尤物| 久久精品免费全国观看国产| 美女视频黄a视频全免费网站色| 国产亚洲av片在线观看18女人| **一级一级毛片免费观看| 国产精品成人亚洲| 亚洲国产综合专区在线电影| 狠狠久久永久免费观看| 国产拍拍拍无码视频免费| 亚洲欧美黑人猛交群| 德国女人一级毛片免费|