emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 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 閱讀(1978) 評(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)論
            

          主站蜘蛛池模板: 聂拉木县| 赤水市| 西峡县| 三穗县| 平昌县| 古蔺县| 营山县| 凤山市| 集安市| 仁寿县| 富源县| 乌鲁木齐县| 新巴尔虎右旗| 无为县| 蒙阴县| 云龙县| 永修县| 名山县| 岑溪市| 绿春县| 神农架林区| 明光市| 沙坪坝区| 古浪县| 商洛市| 云阳县| 巩义市| 邢台县| 永吉县| 马边| 商城县| 铜山县| 申扎县| 贺州市| 吉林省| 晋宁县| 都兰县| 固安县| 福建省| 钟山县| 平武县|