emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

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

          評論

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

          # 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
            回復  更多評論
            

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

          }  回復  更多評論
            

          主站蜘蛛池模板: 清流县| 安庆市| 济源市| 宁德市| 延吉市| SHOW| 焉耆| 海门市| 乌兰县| 奈曼旗| 揭东县| 临泽县| 莎车县| 古丈县| 方正县| 万州区| 裕民县| 临泽县| 阳西县| 南丹县| 社旗县| 温州市| 玉田县| 阿坝县| 巴彦淖尔市| 香河县| 吴川市| 咸宁市| 左贡县| 建湖县| 澄江县| 长葛市| 吉木乃县| 兰西县| 冷水江市| 盘山县| 胶州市| 鹤峰县| 阳朔县| 拉萨市| 周至县|