emu in blogjava

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 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 閱讀(1695) 評(píng)論(10)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評(píng)論

          # emu的解法 2005-08-23 16:51 emu
          這個(gè)題基本上就是我以前寫的 java版本的escape和unescape函數(shù) 的簡(jiǎn)化版了。
          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();
              }
          }
            回復(fù)  更多評(píng)論
            

          # re: URLParser(入圍賽250分真題) 2005-08-23 22:35 coordinator
          其實(shí)你應(yīng)該向google提一份簡(jiǎn)歷的
          他們不來(lái)這邊辦比賽真是失誤  回復(fù)  更多評(píng)論
            

          # re: URLParser(入圍賽250分真題) 2005-08-25 09:18 emu
          google的比賽誰(shuí)都可以參加啊,為什么一定要來(lái)中國(guó)辦呢?考場(chǎng)里面也見(jiàn)到不少中國(guó)人。  回復(fù)  更多評(píng)論
            

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

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


          我還是用正則表達(dá)式做的。

          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));
           }
          }
            回復(fù)  更多評(píng)論
            

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

          這兩個(gè)是樣本本身的錯(cuò)誤。 escape后的數(shù)據(jù)是絕對(duì)不會(huì)出現(xiàn) “%--” 和“%4X”這樣的字符串的。因?yàn)椤?”作為轉(zhuǎn)義符,它不能用來(lái)表示“%”自己。你的parse結(jié)果吧它當(dāng)成自己,其實(shí)也只是掩蓋了問(wèn)題而已。

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

          為什么你只留名字不留聯(lián)系方式啊,搞的好像地下黨單線聯(lián)系一樣。  回復(fù)  更多評(píng)論
            

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

          那個(gè)大寫的P是我copy進(jìn)來(lái)的時(shí)候不小心敲掉了,手工補(bǔ)了一個(gè),也沒(méi)管大小寫,看來(lái)是自己依賴機(jī)器校驗(yàn)慣了,不好意思。

          如果你要我的聯(lián)系方式,那我的email是liouxiao_AT_gmail_DOT_com
          不過(guò)我覺(jué)得這里就是一個(gè)很好的交流平臺(tái)啊。  回復(fù)  更多評(píng)論
            

          # 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));
            }
          }

          正則如果不能直接進(jìn)行全局replace的話,還沒(méi)有自己實(shí)現(xiàn)替換來(lái)的快。  回復(fù)  更多評(píng)論
            

          # re: URLParser(入圍賽250分真題) 2005-12-09 23:26 drekar
          這里的代碼就是照著replaceAll改的,事實(shí)上就算用replaceAll也不會(huì)快多少,這主要是前面用到的正則表達(dá)式判斷花的時(shí)間長(zhǎng)。
          如果樣本不會(huì)出現(xiàn)我所舉的例子,那確實(shí)沒(méi)必要用正則表達(dá)式;但是如果轉(zhuǎn)換的規(guī)則再?gòu)?fù)雜一些,也許實(shí)現(xiàn)上可以更靈活呢。  回復(fù)  更多評(píng)論
            

          # re: URLParser(入圍賽250分真題) 2005-12-09 23:29 drekar
          這里的代碼就是照著replaceAll改的,事實(shí)上就算用replaceAll也不會(huì)快多少,這主要是前面用到的正則表達(dá)式判斷花的時(shí)間長(zhǎng)。
          如果樣本不會(huì)出現(xiàn)我所舉的例子,那確實(shí)沒(méi)必要用正則表達(dá)式;但是如果轉(zhuǎn)換的規(guī)則再?gòu)?fù)雜一些,也許實(shí)現(xiàn)上可以更靈活呢。

          (剛發(fā)現(xiàn)我用firefox1.5無(wú)法提交留言,是blogjava的bug嗎?)  回復(fù)  更多評(píng)論
            

          # re: URLParser(入圍賽250分真題) 2005-12-10 00:52 emu
          有一個(gè)現(xiàn)成的更復(fù)雜一點(diǎn)點(diǎn)的轉(zhuǎn)換規(guī)則啊,試試javascript里面的escape/unescape:

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

          我的解答前面貼過(guò)了 java版本的escape和unescape函數(shù)  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 浮梁县| 荣昌县| 潼关县| 平潭县| 白山市| 家居| 临西县| 崇信县| 辽源市| 平利县| 台州市| 江油市| 志丹县| 武乡县| 寻乌县| 邵阳县| 安塞县| 英超| 二连浩特市| 武乡县| 定远县| 衡阳县| 绥宁县| 南岸区| 云安县| 永新县| 山东省| 尤溪县| 长汀县| 长兴县| 循化| 荣昌县| 淄博市| 陵水| 个旧市| 乐平市| 安康市| 浦东新区| 平安县| 株洲县| 鄂温|