[双指针] leetcode 1147 Longest Chunked Palindrome Decomposition

problem:https://leetcode.com/contest/weekly-contest-148/problems/longest-chunked-palindrome-decomposition/

        Week tournament title. First and last pointers before and after the detected character string is equal, if the two are equal then the cursor moves to the next matching position; not equal the current position continues to find a match.

class Solution {
public:
    int longestDecomposition(string text) {
        int i = 0;
        int j = text.size() - 1;
        int res = 0;
        while(i < j)
        {
            int old_i = i;
            while(true)
            {            
                if(i == j) return res + 1;
                if(text[i] == text[j])
                {
                    int size = i - old_i + 1;
                    int new_j = j - size + 1;
                    cout << text.substr(old_i, size) << " " << text.substr(new_j, size) << endl;
                    if(new_j <= i)
                    {
                        return res + 1;   
                    }
                    if(text.substr(old_i, size) != text.substr(new_j, size))
                    {
                        
                    }
                    else
                    {
                        j = new_j - 1;
                        i++;
                        res += 2;
                        break;
                    }
                }
                i++;
            }
        }
        if(i == j) return res + 1;
        return res;
    }
};

 

Guess you like

Origin www.cnblogs.com/fish1996/p/11297959.html