<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
    ????
    When a web client (like a browser) requests a particular web page, it typically replaces certain characters with escape sequences. For instance, spaces are replaced with "%20". Your task is to reverse this, replacing every escape sequence in the input with the character it represents. Each escape sequence is formatted as "%XX" where XX is the ASCII value of the escaped character in hexadecimal.
    Definition
    ????
    Class:
    URLParser
    Method:
    parse
    Parameters:
    String
    Returns:
    String
    Method signature:
    String parse(String url)
    (be sure your method is public)
    ????

    Constraints
    -
    url will contain between 1 and 50 characters, inclusive.
    -
    Each '%' in the input will be followed by a hexadecimal value between 20 (hex) and 7E (letters will be uppercase).
    -
    Each character in the input will have ASCII value between 32 and 126, inclusive.
    Examples
    0)

    ????
    "http://www.%20%40%20%40%20.com/%25"
    Returns: "http://www. @ @ .com/%"
    "%20" is the escape sequence for ' ', while "%40" stands for '@' and "%25" stands for '%'.
    1)

    ????
    "%20%21%22%23%24%25%26%27%28"
    Returns: " !\"#$%&'("

    2)

    ????
    "%48%65%6C%6C%6F%20%57%6F%72%6C%64%21"
    Returns: "Hello World!"

    3)

    ????
    "%2525"
    Returns: "%25"

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

    評論

    # emu的解法 2005-08-23 16:51 emu
    這個題基本上就是我以前寫的 java版本的escape和unescape函數 的簡化版了。
    public class URLParser {
        public static void main(String[] args)
        {
            URLParser u = new URLParser();
                System.out.println(u.parse("%48%65%6C%6C%6F%20%57%6F%72%6C%64%21"));
        }
        public String parse(String url){
            StringBuffer tmp = new StringBuffer();
            tmp.ensureCapacity(url.length());
            int lastPos = 0, pos = 0;
            char ch;
            while (lastPos < url.length()) {
                pos = url.indexOf("%", lastPos);
                if (pos == lastPos) {
        ch = (char) Integer.parseInt(url.substring(pos + 1, pos + 3),16);
        tmp.append(ch);
        lastPos = pos + 3;
                } else {
                    if (pos == -1) {
                        tmp.append(url.substring(lastPos));
                        lastPos = url.length();
                    } else {
                        tmp.append(url.substring(lastPos, pos));
                        lastPos = pos;
                    }
                }
            }
            return tmp.toString();
        }
    }
      回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-08-23 22:35 coordinator
    其實你應該向google提一份簡歷的
    他們不來這邊辦比賽真是失誤  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-08-25 09:18 emu
    google的比賽誰都可以參加啊,為什么一定要來中國辦呢?考場里面也見到不少中國人。  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 16:16 drekar
    這幾個測試樣本會出錯:

    "--%--%48%65%6C"
    "%4X%65%6C"


    我還是用正則表達式做的。

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class URLParser {

     Public String parse(String url) {
      if (null == url)
       return "";

      Pattern p = Pattern.compile("%[0-9A-Fa-f]{2}");
      Matcher m = p.matcher(url);
      boolean found = m.find();
      if (found) {
       StringBuffer sb = new StringBuffer();
         do {
          String temp = m.group(0).substring(1);
          char a = (char)Integer.parseInt(temp, 16);
           m.appendReplacement(sb, ""+a);
           found = m.find();
         } while (found);
         m.appendTail(sb);
         return sb.toString();
      } else
       return url;
     }

     public static void main(String[] args) {
      String url = "%48%65%6C%6C%6F%20%57%6F%72%6C%64%21";
      URLParser up = new URLParser();
      System.out.println(up.parse(url));
     }
    }
      回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 16:34 emu
    >這幾個測試樣本會出錯:
    >"--%--%48%65%6C"
    >"%4X%65%6C"

    這兩個是樣本本身的錯誤。 escape后的數據是絕對不會出現 “%--” 和“%4X”這樣的字符串的。因為“%”作為轉義符,它不能用來表示“%”自己。你的parse結果吧它當成自己,其實也只是掩蓋了問題而已。

    你的代碼里面  Public String parse(String url) { 第一個字母P怎么大寫了?

    為什么你只留名字不留聯系方式啊,搞的好像地下黨單線聯系一樣。  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 17:13 drekar
    哦。
    我看題目給的樣本也不全是URL,就自己編了幾個,嘿嘿。。。

    那個大寫的P是我copy進來的時候不小心敲掉了,手工補了一個,也沒管大小寫,看來是自己依賴機器校驗慣了,不好意思。

    如果你要我的聯系方式,那我的email是liouxiao_AT_gmail_DOT_com
    不過我覺得這里就是一個很好的交流平臺啊。  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 18:31 emu
    試試看你的程序比我的慢幾倍?

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class URLParser {

      public String drekar(String url) {
        if (null == url)
          return "";

        Pattern p = Pattern.compile("%[0-9A-Fa-f]{2}");
        Matcher m = p.matcher(url);
        boolean found = m.find();
        if (found) {
          StringBuffer sb = new StringBuffer();
              do {
                String temp = m.group(0).substring(1);
                char a = (char)Integer.parseInt(temp, 16);
                  m.appendReplacement(sb, ""+a);
                  found = m.find();
              } while (found);
              m.appendTail(sb);
              return sb.toString();
        } else
          return url;
      }
        public String emu(String url){
            StringBuffer tmp = new StringBuffer();
            tmp.ensureCapacity(url.length());
            int lastPos = 0, pos = 0;
            char ch;
            while (lastPos < url.length()) {
                pos = url.indexOf("%", lastPos);
                if (pos == lastPos) {
        ch = (char) Integer.parseInt(url.substring(pos + 1, pos + 3),16);
        tmp.append(ch);
        lastPos = pos + 3;
                } else {
                    if (pos == -1) {
                        tmp.append(url.substring(lastPos));
                        lastPos = url.length();
                    } else {
                        tmp.append(url.substring(lastPos, pos));
                        lastPos = pos;
                    }
                }
            }
            return tmp.toString();
        }

      public static void main(String[] args) {
        String url = "a%20b%20c%20d%20e%20f%20g%20h%20i%20j%20k%20l%20m%20n%20o%20p%20q%20r%20s%20t%20u%20v%20w%20x%20y%20z%20A%20B%20C%20D%20E%20F%20G%20H%20I%20J%20K%20L%20M%20N%20O%20P%20Q%20R%20S%20T%20U%20V%20W%20X%20Y%20Z%20" ;
        URLParser up = new URLParser();
        long t = System.currentTimeMillis();
        for(int i=0;i<1000;i++)
        up.drekar(url);
        System.out.println("drekar time used "+(System.currentTimeMillis()-t));
        t = System.currentTimeMillis();
        for(int i=0;i<1000;i++)
        up.emu(url);
        System.out.println("emu time used "+(System.currentTimeMillis()-t));
      }
    }

    正則如果不能直接進行全局replace的話,還沒有自己實現替換來的快。  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 23:26 drekar
    這里的代碼就是照著replaceAll改的,事實上就算用replaceAll也不會快多少,這主要是前面用到的正則表達式判斷花的時間長。
    如果樣本不會出現我所舉的例子,那確實沒必要用正則表達式;但是如果轉換的規則再復雜一些,也許實現上可以更靈活呢。  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-09 23:29 drekar
    這里的代碼就是照著replaceAll改的,事實上就算用replaceAll也不會快多少,這主要是前面用到的正則表達式判斷花的時間長。
    如果樣本不會出現我所舉的例子,那確實沒必要用正則表達式;但是如果轉換的規則再復雜一些,也許實現上可以更靈活呢。

    (剛發現我用firefox1.5無法提交留言,是blogjava的bug嗎?)  回復  更多評論
      

    # re: URLParser(入圍賽250分真題) 2005-12-10 00:52 emu
    有一個現成的更復雜一點點的轉換規則啊,試試javascript里面的escape/unescape:

    %u5165%u56F4%u8D5B250%u5206%u771F%u9898 《==》 入圍賽250分真題

    我的解答前面貼過了 java版本的escape和unescape函數  回復  更多評論
      

    主站蜘蛛池模板: 国产大片免费观看中文字幕| 色久悠悠婷婷综合在线亚洲| 四虎影视永久在线精品免费| 亚洲精品你懂的在线观看| 69视频在线观看免费| 亚洲成a人无码亚洲成www牛牛| 国产性爱在线观看亚洲黄色一级片| 免费人成毛片动漫在线播放| 国产午夜亚洲精品国产| 三上悠亚亚洲一区高清| 成人免费的性色视频| 一本久久A久久免费精品不卡| 亚洲神级电影国语版| 亚洲国产主播精品极品网红 | 久久成人免费大片| 亚洲最大无码中文字幕| 亚洲国产精品高清久久久| 午夜a级成人免费毛片| 午夜免费福利小电影| 黄色免费网址大全| 亚洲AV无码国产精品色| 久久精品国产亚洲麻豆| 免费中文字幕在线观看| 91免费资源网站入口| 久操视频在线免费观看| 一进一出60分钟免费视频| 亚洲人成未满十八禁网站 | 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 亚洲网址在线观看| 久久久久亚洲AV无码专区桃色| 免费可以在线看A∨网站| 日韩免费无码视频一区二区三区| 特黄aa级毛片免费视频播放| 亚洲av成人一区二区三区| 婷婷精品国产亚洲AV麻豆不片 | 999国内精品永久免费观看| a级毛片高清免费视频就| 黄色一级视频免费| 亚洲风情亚Aⅴ在线发布| 一本色道久久综合亚洲精品蜜桃冫 | 一级女性全黄久久生活片免费|