leetcode 05 The longest string between the longest identical elements && 06 Z-shaped string sorting

05 The longest string between the longest identical elements
At the beginning of this question, because it is immediately below the 03 question, and the examples given are very similar to the 03 question, so with the understanding of the 03 question, I can't help but think of the problem-solving method. The method of taking out the different elements and adding the last same element is considered to be the same type of question as the 03 question. So the following solution was made:
class Solution {
public:
    string longestPalindrome(string s) {
        string max;
        string tmp;
        if(s.size()==1)
            return s;
         vector<int> flag(256,0);
        int start=0;
        for(int i=0;i<s.length();i++){
            if(flag[s[i]]>start)
                start=flag[s[i]];
            flag[s[i]]=i;
          tmp=s.substr(start,i-start);
           for(int j=i+1;j<s.length();j++){
               int n=0;
               if(s[j]!=s[i])
                   break;
               n++;
               tmp=s.substr(i,n+2);  
           }
            if(tmp.length()>max.length())
                max=tmp;
        }
        return max;
    }
};

Referring to the impressive method of recording the position of the string in question 03, it can be solved very well for the special case of the example, but when the last string is the largest, the solution is wrong. I read the question and found that This idea is completely wrong. The idea of ​​solving the problem should be to output the largest string between two identical elements, not the largest string between two consecutive identical strings. The correct solution is:
class Solution {
public:
    string longestPalindrome(string s) {  
    if(s.empty()) return"";
        if(s.size()==1) return s;
        int min=0;
        int maxLen=1;
        for(int i=0;i<s.size();){
            if(s.size()-i<=maxLen/2)
                break;
            int j=i,k=i;
            while(k<s.size()-1 && s[k]==s[k+1])++k;
            i=k+1;
            while(k<s.size()-1 && j>0 && s[j-1]==s[k+1]){
                --j;
                ++k;
            }
            int nLen=k-j+1;
            if(nLen>maxLen){
                min = j;
                maxLen=nLen;
            }
        }
        return s.substr(min,maxLen);
    }
};

Skip the same elements, search for strings by binary search, and record the largest string as the solution to the problem.
06 Z-shaped string sorting

I didn't understand this question at the beginning, and I didn't quite understand what the given string table meant. So I directly looked at the output result and the input value, and just found that the first element of the output string and the second element of the output string are separated by 3 elements in the input string, and each element in the second line is like a 1 element. The three lines are still the same, so I think that the interval for odd-numbered rows is nRows+1, and the interval for even-numbered rows is nRows/2+1, so I came up with the following solution:
class Solution {
public:
    string convert(string s, int numRows) {
        string answer;
        if(s.empty())
        return "";
        for(int i=0;i<numRows;i++){
            if(i%2==0){
                for(;i<s.size();){
                answer.append(&s[i]);
                i=i+numRows+1;
                }  
            }
            else{
                for(;i<s.size();){
                answer.append(&s[i]);
                i=i+numRows/2+1;
                }
            }
           
        }
       return answer;
    }
};

However, there was an error in the submission, and the problem can be solved corresponding to the special solution, so re-reading the question found that the string should not be read from left to right, but the input string should be read from top to bottom, and then output from left to right, and Interleaving inserts a single element in the middle row, so the problem-solving error is gorgeous again. But I thought that for even-numbered lines such as 2 lines, the question did not clearly indicate what the situation was at the time, so I went to the solution to find it, and found an easy-to-understand, inductive solution:
For each line element in the title there are:

For the first row [0, 2m-2, 4m-4, 6m-6....] for
the last row [m-1, 3m-3, 5m-5]
for the middle i row number [i, 2m-2 -i, 2m-2+i, 4m-4-i, 4m-4+i...], so according to this rule, the procedure is:
class Solution {
public:
    string convert(string s, int numRows) {
        string answer;
        int i=0,j;
        if(numRows==1) return s;
        for(i=0;i<numRows;i++){
            j=0;
            while(j<s.size()+i){
                if(i<numRows-1 && i>0 && j>i)
                    answer+=s.at(j-i);
                if((i+j)<s.size())
                    answer+=s.at(j+i);
                j+=2*numRows-2;
            }
        }
        return answer;
        }
    };

I made the same mistake for these two questions, that is, I made subjective assumptions based on the special cases given without carefully reviewing the questions, so most of the results obtained were special solutions, while for general and regular programs, no correct solutions were given. , the review is still very important!

I have to complain about why the blog article will be automatically deleted after the CSDN draft is deleted! Shouldn't it be a copy? I thought I could delete the draft after I posted it, but I didn't expect that after I deleted it completely, I didn't even publish the article. So it's really a bit sad to write two copies of the same thing. I feel that friends who are less patient may delete it directly. Some people may ask why do you want to save the draft? I don't want to either. The blog page suddenly slammed and couldn't insert pictures. I wanted to write the two together, so I saved it and refreshed it again! As a result, this kind of problem still occurs, so let me reason with whom. . . . So don't delete the drafts of the majority of writers! Maybe the article is directly 404.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728979&siteId=291194637