<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
    ????
    An image editing application allows users to construct images containing multiple layers. When dealing with large images, however, it is sometimes necessary to limit the number of layers due to memory constraints. If certain layers will not be altered during an editing session, they can be merged together to reduce the total number of layers in memory. You are given a macro containing commands to open files and merge layers. Each time a file is opened, it is loaded into a new layer of the current image. Layers are numbered starting at 0 for the bottommost layer, 1 for the layer directly on top of the bottommost layer, etc... Whenever a new layer is created, it is positioned on top of the previous topmost layer. An open command is formatted as "OPEN filename" where filename is the name of the file to open. Consecutive layers can be merged using the merge command, which is formatted as "MERGE layer1-layer2", where layer1 and layer2 specify an inclusive range of layer numbers that exist in the current image. After multiple layers are merged, all the layers are renumbered according to the original specification. For example, if an image contains four layers (0, 1, 2, 3), and layers 1 and 2 are merged into a single layer, the final image will contain three layers numbered 0, 1, 2 from bottom to top, where layer 0 is the same as before, layer 1 was previously layers 1 and 2, and layer 2 was previously layer 3.

    Given the String[] macro, perform all the operations in the macro in order and return the final state of the image layers as a String[]. The String[] should contain exactly the same number of elements as there are layers in the final image, and each element i should correspond to the ith layer. Each element of the String[] should be a single space delimited list of the filenames contained in that layer. The filenames should be sorted in alphabetical order within each layer.
    Definition
    ????
    Class:
    ImageLayers
    Method:
    contents
    Parameters:
    String[]
    Returns:
    String[]
    Method signature:
    String[] contents(String[] macro)
    (be sure your method is public)
    ????

    Constraints
    -
    macro will contain between 1 and 50 elements, inclusive.
    -
    Each element of macro will be formatted as either "OPEN filename" or "MERGE layer1-layer2".
    -
    Each filename in macro will contain between 1 and 15 lowercase letters ('a'-'z'), inclusive.
    -
    Each layer1 and layer2 in macro will be integers between 0 and n-1, inclusive, with no leading zeros, where n is the number of layers that exist in the image immediately before the command is executed.
    -
    Within each element of macro that represents a merge command, layer1 will be less than layer2.
    -
    The first element of macro will be an OPEN command.
    Examples
    0)

    ????
    {"OPEN background",
     "OPEN aone",
     "OPEN atwo",
     "OPEN foreground",
     "MERGE 0-2",
     "OPEN border"}
    Returns: {"aone atwo background", "foreground", "border" }
    After the first four commands in macro are executed, the layers are (from bottom to top):

    0. background 1. aone 2. atwo 3. foreground

    The merge command combines the bottom three layers into a single layer. There are now only two layers (from bottom to top):

    0. background aone atwo 1. foreground

    Finally, one last file is opened and placed in a new layer on top. The final return value contains the filenames within each layer sorted alphabetically:

    0. aone atwo background 1. foreground 2. border
    1)

    ????
    {"OPEN sky",
     "OPEN clouds",
     "OPEN ground",
     "MERGE 0-1",
     "OPEN grass",
     "MERGE 0-2",
     "OPEN trees",
     "OPEN leaves",
     "OPEN birds",
     "MERGE 1-2",
     "MERGE 0-1"}
    Returns: {"clouds grass ground leaves sky trees", "birds" }

    2)

    ????
    {"OPEN a", "OPEN b", "OPEN a", "OPEN a", "MERGE 0-3"}
    Returns: {"a a a b" }

    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:44 emu 閱讀(1677) 評論(12)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

    # emu的答案 2005-08-23 11:45 emu
    import java.util.*;
    public class ImageLayers {
        public static void main(String[] args) {
            String[] macro = new String[] {"OPEN sky",
                             "OPEN clouds",
                             "OPEN ground",
                             "MERGE 0-1",
                             "OPEN grass",
                             "MERGE 0-2",
                             "OPEN trees",
                             "OPEN leaves",
                             "OPEN birds",
                             "MERGE 1-2",
                             "MERGE 0-1"}
    ;
            ImageLayers il = new ImageLayers();
            String[] contents = il.contents(macro);
            for (int i=0;i<contents.length;i++)
                System.out.println(contents[i]);
                            

        }

        public String[] contents(String[] macro){
            ArrayList layers = new ArrayList();
            for (int i=0;i<macro.length;i++){
                String command = macro[i];
                if (command.startsWith("OPEN")){
                    ArrayList layer = new ArrayList();
                    layer.add(command.split(" ")[1]);
                    layers.add(layer);
                }
                else if (command.startsWith("MERGE")){
                    String[] l = command.split(" ")[1].split("-");
                    int layer1 = Integer.parseInt(l[0],10);
                    int layer2 = Integer.parseInt(l[1],10);
                    ArrayList tmpLayers = (ArrayList)layers.get(layer1);                
                    for (int j=layer2;j>layer1;j--)
                        tmpLayers.addAll((ArrayList)layers.remove(j));
                }
            }
            String[] result = new String[layers.size()];
            for (int i=0;i<result.length;i++){
                Object[] stAr = ((ArrayList)layers.get(i)).toArray();
                Arrays.sort(stAr);
                String s =  Arrays.toString(stAr).replace(',',' ');
                result[i] = s.substring(1,s.length()-1);
            }

            return result;
        }

    }
      回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-08-25 11:01 emu
    遺憾的很,我這個答案沒有通過google的系統(tǒng)測試。  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-09 09:55 chenrenbo
    你好像沒有對結(jié)果排序  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-09 10:54 emu
    Arrays.sort(stAr); 不是排序是什么?  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-09 14:42 drekar
    你的排序結(jié)果是用2個空格做間隔的("clouds<space><space>grass"),可能系統(tǒng)測試期望結(jié)果是1個空格("clouds<space>grass")。

    把 String s = Arrays.toString(stAr).replace(",", "");
    改成 String s = Arrays.toString(stAr).replace(",", "");
    應(yīng)該就好了。

    下面是我的解法:

    import java.util.ArrayList;
    import java.util.Arrays;

    public class ImageLayers {

     ArrayList canvas = new ArrayList();
     
     public String[] contents(String[] macro) {
      if (null == macro)
       return new String[0];

      for (int i=0; i<macro.length; i++) {
       String[] temp = macro[i].split(" ");
       if (temp[0].equalsIgnoreCase("OPEN"))
        open(temp[1]);
       else
        merge(temp[1]);
      }
      
      String [] result = new String[canvas.size()];
      for (int i=0; i<canvas.size(); i++)
       result[i] = (String)canvas.get(i);
      return result;
     }
     
     public void open(String filename) {
      canvas.add(filename);
     }
     
     public void merge(String layers) {
      if (null == layers)
       return;
      
      String[] temp = layers.split("-");
      int baseLayer = Integer.parseInt(temp[0]);
      int lastLayer = Integer.parseInt(temp[1]);
      if (baseLayer >= lastLayer || lastLayer >= canvas.size())
       return;
      
      String newLayerName = (String)canvas.get(baseLayer);
      canvas.remove(baseLayer);
      for (int i=baseLayer+1; i<= lastLayer; i++) {
       newLayerName += " " + canvas.get(baseLayer);
       canvas.remove(baseLayer);
      }
      temp = newLayerName.split(" ");
      Arrays.sort(temp);
      newLayerName = temp[0];
      for (int i=1; i<temp.length; i++)
       newLayerName += " " + temp[i];

      canvas.add(baseLayer, newLayerName);
     }
     
     /**
      * @param args
      */
     public static void main(String[] args) {
      ImageLayers il = new ImageLayers();
      String[] macro = { "OPEN sky", "OPEN clouds", "OPEN ground",
        "MERGE 0-1", "OPEN grass", "MERGE 0-2", "OPEN trees",
        "OPEN leaves", "OPEN birds", "MERGE 1-2", "MERGE 0-1" };
      String[] result = il.contents(macro);
      
      for (int i=0; i<result.length; i++)
       System.out.println(result[i]);
     }
    }

    每次merge都排序,還是沒有你的代碼優(yōu)化。  回復(fù)  更多評論
      

    # 上個貼子有錯 2005-12-09 14:44 drekar
    第3-4行應(yīng)該是『

    把 String s = Arrays.toString(stAr).replace(',', ' ');
    改成 String s = Arrays.toString(stAr).replace(",", "");


    』, :)  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-09 14:50 emu
    這道題當時沒有得分,恐怕不只是沒有排序,還有這個空格的問題。  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-11 11:51 john_wang71
    試試看這個,完全用java.util.*中的類實現(xiàn):
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Vector;

    public class ImageLayers
    {
    public String[] contents(String[] macro)
    {
    String[] s = new String[] {};
    try
    {
    Vector v = new Vector();
    for (int i = 0; i < macro.length; i++)
    {
    String m = macro[i];
    switch (m.charAt(0))
    {
    case 'O':
    List c = new ArrayList();
    c.add(m.substring(5));
    v.add(c);
    break;
    case 'M':
    int pos = m.indexOf('-');
    int l1 = Integer.parseInt(m.substring(6, pos));
    List c1 = (List) v.elementAt(l1);
    int l2 = Integer.parseInt(m.substring(pos + 1));
    for (int l3 = l2; l3 > l1; l3--)
    {
    List c3 = (List) v.elementAt(l3);
    for (Iterator iter3 = c3.iterator(); iter3.hasNext();)
    c1.add(iter3.next());
    v.remove(l3);
    }
    }
    }
    s = new String[v.size()];
    int i = 0;
    for (Iterator iter = v.listIterator(); iter.hasNext();)
    {
    List c = (List) iter.next();
    Collections.sort(c);
    StringBuffer sb = new StringBuffer();
    boolean first = true;
    for (Iterator iter1 = c.iterator(); iter1.hasNext();)
    {
    if (first)
    first = false;
    else
    sb.append(' ');
    sb.append(iter1.next());
    }
    s[i++] = sb.toString();
    }
    }
    catch (Exception e)
    {
    }
    return s;
    }
    }
      回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-11 17:55 小飛俠
    public class test {
    public static String[] content(String[] macro) {
    String[] mystr;
    int cur, i,j, n, begin, end;
    n = macro.length;
    cur = 0;
    mystr = new String[n];
    //第一步操作,獲取merge的String數(shù)組
    for (i = 0; i < n; i++) {
    if (macro[i].startsWith("OPEN")) {
    mystr[cur] = macro[i].substring(5);
    }

    if (macro[i].startsWith("MERGE")) {
    begin = Integer.parseInt(macro[i].substring(6, macro[i].indexOf("-")));
    end = Integer.parseInt(macro[i].substring(macro[i].indexOf("-")+1));

    //實現(xiàn)合并操作
    cur = begin;
    for (j = begin + 1; j <= end; j++) {
    mystr[cur] = mystr[cur].concat(" ").concat(mystr[j]);
    mystr[j] = null;
    }
    }
    cur++;
    }

    for (i = 0; i < n; i++) {
    System.out.println(mystr[i]);
    }

    //下面將原始的合并后的字符串數(shù)組,排序: 排序后,返回真正的數(shù)組
    mystr = sorts(mystr);

    System.out.println("sort ===");
    for (i = 0; i < mystr.length; i++) {
    System.out.println(mystr[i]);
    }

    return mystr;
    }

    public static String[] sorts(String[] mystr) {
    String[] ret;
    int i, j, n, cnt, cur=0;

    n = mystr.length;
    cnt = 0;

    //獲取實際的數(shù)組單元
    for(i = 0, n = mystr.length; i < n; i++) {
    if (mystr[i] != null)
    cnt++;

    }

    ret = new String[cnt];

    for (i = 0; i < n; i++) {
    //對單元進行排序
    if (mystr[i] != null) {
    ret[cur++] = sort(mystr[i]);
    }

    }


    System.out.println(cnt);
    return ret;
    }

    //單元排序, 最簡單的冒泡排序
    public static String sort(String str) {
    String[] a;
    int i, j,n, flag;
    String temp;

    if (str.indexOf(" ") < 0) {
    return str;
    }

    //開始排序
    a = str.split(" ");
    n = a.length;
    for (i = 1; i < n; i++) {
    flag = 0;
    for (j = 0; j < (n - i) ; j++) {
    if (a[j].compareTo(a[j+1]) > 0) {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    flag = 1;
    }
    }

    if (flag == 0) {
    break;
    }
    }

    //merge
    str = a[0];
    for (i = 1; i < n; i++) {
    str = str.concat(" ").concat(a[i]);
    }

    return str;
    }

    public static void main(String args[]) {
    String[] macro = {"OPEN sky",
    "OPEN clouds",
    "OPEN ground",
    "MERGE 0-1",
    "OPEN grass",
    "MERGE 0-2",
    "OPEN trees",
    "OPEN leaves",
    "OPEN birds",
    "MERGE 1-2",
    "MERGE 0-1"};

    content(macro);

    }
    }  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-12 09:12 emu
    不贊成自己實現(xiàn)排序  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-12 11:49 小飛俠
    哈,是的:)

    把自己的排序,改成Arrays.sort(split) :)

    嘻嘻,今天要比賽了,祝福各位,祝福emu, 也祝福我,小飛俠:)嘻嘻  回復(fù)  更多評論
      

    # re: ImageLayers(入圍賽750分真題) 2005-12-12 15:23 emu
    750分題的時間復(fù)雜度降不下來,看來通不過資格賽了,可惜……  回復(fù)  更多評論
      

    主站蜘蛛池模板: 色婷五月综激情亚洲综合| 三上悠亚在线观看免费| 免费在线观看自拍性爱视频| 久久精品免费观看国产| 亚洲av无码成人精品区| 亚洲另类精品xxxx人妖| 久久精品免费视频观看| 亚洲色大成网站www尤物| 日本亚洲免费无线码| 精品亚洲成AV人在线观看| 中美日韩在线网免费毛片视频| 成人免费视频观看无遮挡| 91亚洲国产在人线播放午夜| 在线成人精品国产区免费| 亚洲精品无码Av人在线观看国产| 草久免费在线观看网站| 国产成人亚洲综合无码| 中文字幕av无码不卡免费| 国产日本亚洲一区二区三区| 在线日韩日本国产亚洲| 国产亚洲精品免费视频播放| 在线亚洲高清揄拍自拍一品区| 日本XXX黄区免费看| 岛国精品一区免费视频在线观看 | 亚洲一卡2卡4卡5卡6卡残暴在线| 十八禁无码免费网站| 99久久精品国产亚洲| 亚洲国产精品人人做人人爱| 亚洲第一成年免费网站| 日韩精品免费一线在线观看| 亚洲永久在线观看| 亚洲国语精品自产拍在线观看 | 国产免费一区二区三区| 麻豆狠色伊人亚洲综合网站 | 美女被羞羞网站免费下载| 又黄又大又爽免费视频| 尤物视频在线免费观看| 亚洲一线产品二线产品| 亚洲另类图片另类电影| 78成人精品电影在线播放日韩精品电影一区亚洲| 亚洲AV无码乱码在线观看|