Square Subsequence

前端 未结 5 479
盖世英雄少女心
盖世英雄少女心 2020-12-28 08:36

A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, \"abab\", \"aa\" are square strings, while \"aaa\", \"a

5条回答
  •  庸人自扰
    2020-12-28 09:24

    Psuedocode:

    total_square_substrings <- 0
    
    # Find every substring
    for i in 1:length_of_string {
    
        # Odd strings are not square, continue
        if((length_of_string-i) % 2 == 1)
            continue;
    
        for j in 1:length_of_string {
            # Remove i characters from the string, starting at character j
            substring <- substr(string,0,j) + substr(string,j+1,length_of_string);
    
            # Test all ways of splitting the substring into even, whole parts (e.g. if string is of length 15, this splits by 3 and 5)
            SubstringTest: for(k in 2:(length_of_substring/2))
            {           
                if(length_of_substring % k > 0)
                    continue;
    
                first_partition <- substring[1:partition_size];
    
                # Test every partition against the first for equality, if all pass, we have a square substring
                for(m in 2:k)
                {           
                    if(first_partition != substring[(k-1)*partition_size:k*partition_size])
                        continue SubstringTest;
                }
    
                # We have a square substring, move on to next substring
                total_square_substrings++;
                break SubstringTest;
            }
        }
    }
    

提交回复
热议问题