简体   繁体   English

最长的子串回文问题

[英]Longest Substring Palindrome issue

I feel like I've got it almost down, but for some reason my second test is coming up with a shorter palindrome instead of the longest one. 我觉得我几乎失意了,但由于某种原因,我的第二次测试得出的是较短的回文而不是最长的回文。 I've marked where I feel the error may be coming from, but at this point I'm kind of at a loss. 我已经标记了我认为错误可能来自哪里,但此时我有点不知所措。 Any direction would be appreciated! 任何方向将不胜感激!

#include <stdio.h>
#include <string.h>

/*
 *  Checks whether the characters from position first to position last of the string str form a palindrome.
 *  If it is palindrome it returns 1.  Otherwise it returns 0.
 */
int isPalindrome(int first, int last, char *str)
{
    int i;

    for(i = first; i <= last; i++){
        if(str[i] != str[last-i]){
            return 0;
        }
    }
    return 1;
}

/*
 *  Find and print the largest palindrome found in the string str.  Uses isPalindrome as a helper function.
 */
void largestPalindrome(char *str)
{
    int i, last, pStart, pEnd;
    pStart = 0;
    pEnd = 0;
    int result;

    for(i = 0; i < strlen(str); i++){
        for(last = strlen(str); last >= i; last--){
            result = isPalindrome(i, last, str);
            //Possible error area
            if(result == 1 && ((last-i)>(pEnd-pStart))){
                pStart = i;
                pEnd = last;
            }
        }
    }
    printf("Largest palindrome: ");
    for(i = pStart; i <= pEnd; i++)
        printf("%c", str[i]);
    return;
}

/*
 *  Do not modify this code.
 */
int main(void)
{
    int i = 0;
    /* you can change these strings to other test cases but please change them back before submitting your code */
    //str1 working correctly
    char *str1 = "ABCBACDCBAAB";
    char *str2 = "ABCBAHELLOHOWRACECARAREYOUIAMAIDOINEVERODDOREVENNGGOOD";

    /* test easy example */
    printf("Test String 1: %s\n",str1);
    largestPalindrome(str1);

    /* test hard example */
    printf("\nTest String 2: %s\n",str2);
    largestPalindrome(str2);

    return 0;
}

Your code in isPalindrome doesn't work properly unless first is 0. 除非first为0,否则isPalindrome中的代码无法正常工作。

Consider isPalindrome(6, 10, "abcdefghhgX") : 考虑isPalindrome(6, 10, "abcdefghhgX")

  • i = 6 ; i = 6 ;
  • last - i = 4; last - i = 4;
  • comparing str[i] (aka str[6] aka 'g' ) with str[last-i] (aka str[4] aka 'e' ) is comparing data outside the range that is supposed to be under consideration. str[i] (aka str[6] aka'g 'g' )与str[last-i] (又名str[4] aka'e 'e' )进行比较,比较应该考虑的范围之外的数据。
  • It should be comparing with str[10] (or perhaps str[9] — depending on whether last is the index of the final character or one beyond the final character). 它应该与str[10] (或者str[9] - 取决于last是最终字符的索引还是最终字符之外的索引。

You need to revisit that code. 您需要重新访问该代码。 Note, too, that your code will test each pair of characters twice where once is sufficient. 另请注意,您的代码将在一次足够的情况下对每对字符进行两次测试。 I'd probably use two index variables, i and j , set to first and last . 我可能会使用两个索引变量ij ,设置为firstlast The loop would increment i and decrement j , and only continue while i is less than j . 循环将递增i并递减j ,并且仅在i小于j继续。

for (int i = first, j = last; i < j; i++, j--)
{
     if (str[i] != str[j])
         return 0;
}
return 1;

isPalindrome ,将if(str[i] != str[last-i]){替换为if(str[i] != str[first+last-i]){

Here's your problem: 这是你的问题:

for(i = first; i <= last; i++){
    if(str[i] != str[last-i]){
        return 0;
    }
}

Should be: 应该:

for(i = first; i <= last; i++, last--){
    if(str[i] != str[last]){
        return 0;
    }
}

Also, this: 这个:

for(last = strlen(str); last >= i; last--){

Should be: 应该:

for(last = strlen(str) - 1; last >= i; last--){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM