emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 2 Trackbacks
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          公告

          常用鏈接

          留言簿(92)

          隨筆分類(20)

          隨筆檔案(171)

          文章分類(89)

          文章檔案(103)

          相冊(cè)

          收藏夾(46)

          友情連接

          收藏

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          Problem Statement

          ????

          A palindrome is a string that reads the same forward and backward. A palindrome substring is a contiguous sequence of characters taken from a string that form a palindrome. A palindrome substring of a string is maximal if we can't extend it to get a bigger palindrome substring. For example, string "acdadc" has 2 maximal palindrome substrings - "a" (the first one) and "cdadc". All other palindrome substrings (like "dad" or "c") can be extended to "cdadc", so they are not maximal.

          You will be given a String[] str. Concatenate the elements of str to form one long string, and return the number of maximal palindrome substrings contained in that string.

          Definition

          ????
          Class: MaximalPalindromeSubstrings
          Method: countMaximalPalindromeSubstrings
          Parameters: String[]
          Returns: int
          Method signature: int countMaximalPalindromeSubstrings(String[] str)
          (be sure your method is public)
          ????

          Constraints

          - str will contain between 1 and 50 elements, inclusive.
          - Each element of str will contain between 1 and 50 characters, inclusive.
          - Each character of each element of str will be a lowercase letter ('a'-'z').

          Examples

          0)
          ????
          {"acdadc"}
          Returns: 2
          The example from the problem statement.
          1)
          ????
          {"ababab"}
          Returns: 2
          The two maximal palindrome substrings here are "ababa" and "babab".
          2)
          ????
          {"aaaa","bbb","axxx"}
          Returns: 3
          Remember to use the whole input!
          3)
          ????
          {"abacabbacaacdbdcacxcbbbbcabababacccbazhhaahh"}
          Returns: 14

          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 2006-09-05 10:20 emu 閱讀(1976) 評(píng)論(3)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評(píng)論

          # re: 500分模擬題 MaximalPalindromeSubstrings 2006-10-17 15:53 oldglost
          是不是用動(dòng)態(tài)規(guī)劃來做?
          任何一個(gè)符合題意的字串,要么有一個(gè)中心字符,要么兩個(gè)兩個(gè)相鄰字符相同
          因此,
          1,把每個(gè)字符,或者每兩個(gè)相鄰字符相同的pair記錄到一個(gè)集合
          2,對(duì)1的集合中所有元素,向左右個(gè)擴(kuò)展一個(gè)字符,如果這兩個(gè)字符相同則記錄,不同則計(jì)數(shù),并扔出集合。
          循環(huán)直到全部元素被扔出。
            回復(fù)  更多評(píng)論
            

          # re: 500分模擬題 MaximalPalindromeSubstrings 2006-11-19 18:05 zwgoal
          我的python解法:


          def MaximalPalindromeSubstrings(s):
          xxflag=-1
          count=0
          for i in range(0,len(s)):
          sta=i
          sed=flag=len(s)-1

          while sed>sta and flag>xxflag:

          if s[sta]==s[sed]:
          sed-=1
          sta+=1
          continue
          else:
          sta=i
          sed=flag
          sed-=1
          flag-=1
          if flag>xxflag:
          count+=1
          xxflag=flag

          if sta==sed:
          print s[(flag-2*(flag-sta)):(flag+1)]
          if sta>sed:
          print s[((flag-2*(flag-sta))-1):(flag+1)]
          if xxflag==len(s)-1:
          break
          return count
          xx='abacabbacaacdbdcacxcbbbbcabababacccbazhhaahh'
          ret=MaximalPalindromeSubstrings(xx)

          print 'count:',ret
            回復(fù)  更多評(píng)論
            

          # re: 500分模擬題 MaximalPalindromeSubstrings 2007-12-27 14:16 jjww
          public static int countMaximalPalindromeSubstrings(String[] str)
          {

          String target = "";
          int length = str.length;
          for (int i=0; i< length;i++)
          {
          target += str[i];
          }
          length = target.length();

          int head = 0;
          int tail = length -1;

          char c1,c2;
          int head_tmp = head;
          int tail_tmp = tail;
          boolean recover = false;
          int count =0;
          int h_length =0;
          int last_h_length =0;
          boolean finished = false;

          for (;!finished;head++)
          {

          h_length = 0;
          c1 = target.charAt(head);

          head_tmp = head;

          int head1 = head_tmp;
          int tail1 = tail;

          for (tail_tmp = tail;head_tmp<tail_tmp;)
          {
          if (recover)
          {
          h_length=0;
          head_tmp = head1;
          tail1--;
          tail_tmp = tail1 ;
          }

          c1 = target.charAt(head_tmp);
          c2 = target.charAt(tail_tmp);
          if (c1 == c2)
          {
          recover = false;
          head_tmp ++;
          tail_tmp --;
          h_length +=2;
          continue;
          }
          else
          {
          recover = true;

          }
          }
          if (tail1 == length-1)
          {
          finished = true;
          }
          if (head_tmp==tail_tmp)
          {
          h_length += 1;
          }
          else if (Math.abs(head_tmp-tail_tmp) == 2)
          h_length -= 1;

          if (last_h_length < head + h_length)
          {
          count ++;
          last_h_length = h_length+head;
          }
          }


          return count;

          }  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 蒙阴县| 郯城县| 福海县| 建德市| 库伦旗| 安陆市| 博客| 启东市| 广安市| 东丰县| 云梦县| 临澧县| 深水埗区| 盐边县| 西丰县| 新建县| 扎兰屯市| 乌兰浩特市| 邹平县| 西藏| 武冈市| 伊川县| 巴马| 莒南县| 河西区| 乐平市| 雷波县| 科技| 临湘市| 吉隆县| 通榆县| 永修县| 秭归县| 余庆县| 斗六市| 屯留县| 平潭县| 丹阳市| 嵊泗县| 扶余县| 广宗县|