<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    emu in blogjava

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      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 閱讀(1973) 評論(3)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

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

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

    }  回復  更多評論
      

    主站蜘蛛池模板: 免费国产叼嘿视频大全网站| 亚洲精品综合在线影院| 国产在线观看xxxx免费| 亚洲爆乳无码专区| 国产一级淫片免费播放电影| 91免费福利精品国产| 中国亚洲呦女专区| 亚洲好看的理论片电影| 亚洲成?v人片天堂网无码| 男人j进入女人j内部免费网站| 亚洲一区二区三区91| 亚洲第一黄色网址| 女人18毛片a级毛片免费视频| 三年片在线观看免费观看大全动漫 | 亚洲av午夜电影在线观看| 亚洲国产婷婷香蕉久久久久久| 99精品国产成人a∨免费看| 国产激情久久久久影院老熟女免费| 亚洲精品国产手机| 亚洲国产成人久久综合一区77| 最近中文字幕无吗高清免费视频| 最近免费中文字幕大全免费| 毛片免费在线观看| 国产一二三四区乱码免费| 一个人看的免费高清视频日本| 国产成人不卡亚洲精品91| 亚洲AV无码男人的天堂| 亚洲国产成人久久一区二区三区| 国产色在线|亚洲| 亚洲乱码一二三四区国产| 亚洲视频精品在线观看| 国产精品亚洲二区在线观看| 日韩亚洲国产综合久久久| 国产人成免费视频网站| 欧洲一级毛片免费| a级成人免费毛片完整版| a级精品九九九大片免费看| 中国人免费观看高清在线观看二区| 久久久精品国产亚洲成人满18免费网站| 美女被吸屁股免费网站| 无码日韩人妻AV一区免费l|