emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks
          <2005年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          公告

          常用鏈接

          留言簿(92)

          隨筆分類(20)

          隨筆檔案(171)

          文章分類(89)

          文章檔案(103)

          相冊

          收藏夾(46)

          友情連接

          收藏

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜


          Problem Statement
          ????
          When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.  You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.
          Definition
          ????
          Class:
          CursorPosition
          Method:
          getPosition
          Parameters:
          String, int
          Returns:
          int
          Method signature:
          int getPosition(String keystrokes, int N)
          (be sure your method is public)
          ????

          Constraints
          -
          keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
          -
          N will be between 1 and 100, inclusive.
          Examples
          0)

          ????
          "ERLLL"
          10
          Returns: 7
          First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
          1)

          ????
          "EHHEEHLLLLRRRRRR"
          2
          Returns: 2
          All the right-arrows at the end ensure that we end up at the end of the line.
          2)

          ????
          "ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
          10
          Returns: 3

          3)

          ????
          "RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
          19
          Returns: 12

          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.



          沒有難度

          public class CursorPosition {
          public int getPosition(String keystrokes, int N){
              
          int p = 0;
              
          if(keystrokes.lastIndexOf("H")>-1)
                  keystrokes
          =keystrokes.substring(keystrokes.lastIndexOf("H")+1);
              
          if(keystrokes.lastIndexOf("E")>-1){
                  keystrokes
          =keystrokes.substring(keystrokes.lastIndexOf("E")+1);
                  p
          =N;
              }

              
          for(int i=0;i<keystrokes.length();i++){
                  
          if(keystrokes.charAt(i)=='R')
                      p
          +=(p<N)?1:0;
                  
          else 
                      p
          -=(p>0)?1:0;
              }

              
          return p;
          }

          public static void main(String[] args){
              System.out.println(
          new CursorPosition().getPosition("ERLLL",10));
              System.out.println(
          new CursorPosition().getPosition("EHHEEHLLLLRRRRRR",2));
              System.out.println(
          new CursorPosition().getPosition("ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL",10));
              System.out.println(
          new CursorPosition().getPosition("RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL",19));
              System.out.println(
          new CursorPosition().getPosition("R",9));

          }

          }

          posted on 2005-12-01 16:57 emu 閱讀(1610) 評論(6)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評論

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-06 21:31 Merlin Ran
          這樣的題就不用拿來現眼了吧。要真的比賽時,這題也就值100分。  回復  更多評論
            

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-06 22:17 emu
          就是啊,這次中國錦標賽的模擬題比上次東亞大賽的模擬題差的太遠了,完全沒有挑戰性。  回復  更多評論
            

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-07 21:30 Merlin Ran
          我今天一直在后悔上面的評論,還好看來你沒生氣。
          這題就是讓大家熟悉比賽環境的,肯定不代表入圍賽的題目難度。真正的題恐怕不會比NewStation更容易。  回復  更多評論
            

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-08 00:45 emu
          呵呵,對自己有信心,就不會理解成自己現眼了。不過這樣的模擬題對沒看過以前的題目的人有很強的誤導性,都以為1000分就這么好拿,到時就傻眼了。懷疑google想多蒙些人來捧場。
          此外這道題我第一次做也是做錯了的(現眼了),在系統自動測試的時候才發現,原來位置是從0到n,不是從1到n。所以題目容易也要仔細讀題。上次東亞大賽的時候有一道題就是沒有搞清楚輸出結果是要排序的丟了分。  回復  更多評論
            

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-10 00:24 小飛俠
          public class CursorPosition {
          public static int getPosition(String commonds, int ncount) {
          //獲取開始查詢的位置
          int fromIndex1, fromIndex2, fromIndex, position;

          fromIndex1 = commonds.lastIndexOf((int)'E');
          fromIndex2 = commonds.lastIndexOf((int)'H');
          if (fromIndex1 > fromIndex2) {
          fromIndex = fromIndex1 >0 ? fromIndex1 : 0;
          position = ncount;
          } else {
          fromIndex = fromIndex2 > 0 ? fromIndex2 : 0;
          position = 0;
          }

          //取字符串的字符,開始進行統計
          for (int i = fromIndex + 1; i < commonds.length(); i++) {
          System.out.println(commonds.charAt(i));
          switch(commonds.charAt(i)) {
          case 'L' :
          position = (position == 0) ? 0 : --position;
          break;
          case 'R' :
          position = (position == ncount) ? ncount : ++position;
          break;
          default :
          break;
          }
          }

          return position;
          }
          }   回復  更多評論
            

          # re: google中國編程挑戰賽模擬題 -- CursorPosition(1000分) 2005-12-10 00:27 小飛俠
          //簡單的方法
          public class CursorPosition {
          public int getPosition(String commonds, int ncount) {
             int position = 0;
          for (int i = 0; i < commonds.length(); i++) {
          switch(commonds.charAt(i)) {
          case 'H' :
          position = 0;
          break;
          case 'E' :
          position = ncount;
          break;
          case 'R' :
          position = position < ncount ? positon++ : ncount;
          break;
          case 'L' :
          position = position >0 ? position-- : 0;
          break;
          default :
          break;
          }
          } 

          return position;    
            }
          }  回復  更多評論
            

          主站蜘蛛池模板: 沙洋县| 西乡县| 微山县| 会昌县| 曲靖市| 耿马| 金昌市| 南城县| 成安县| 武汉市| 五家渠市| 巴东县| 满城县| 营口市| 南漳县| 镇巴县| 澄江县| 姚安县| 元江| 龙胜| 花莲县| 建平县| 大同县| 嘉峪关市| 吴忠市| 西林县| 泰宁县| 双辽市| 玉门市| 江油市| 进贤县| 岳普湖县| 无棣县| 靖宇县| 舞钢市| 凤山市| 调兵山市| 林芝县| 岢岚县| 天台县| 平度市|