(75) 647. Fumiko skewer (leet code)

	题目链接:
https://leetcode-cn.com/problems/palindromic-substrings/
	难度:中等
647. 回文子串
	给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
	具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
	输入:"abc"
	输出:3
	解释:三个回文子串: "a", "b", "c"
示例 2:
	输入:"aaa"
	输出:6
	解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"
提示:
	输入的字符串长度不会超过 1000 。

How do you say this question? After all, it is still a spicy chicken.
The simplest idea is of course to brute force to enumerate all strings and then determine whether it is a palindrome. The code is as follows

class Solution {
    
    
public:
    int countSubstrings(string s) {
    
    
        int n = s.size();
        int ans=0;
        for(int i=0;i<n;++i){
    
    
            for(int j=i;j<n;++j){
    
    
                if(is(s,i,j)){
    
    
                    ans++;
                }

            }
        }
        return ans;
    }
    bool is(string &s,int i,int j){
    
    
        while(i<=j){
    
    
            if(s[i]!=s[j]){
    
    
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
};

Although the way of thinking is extremely simple, the time complexity is O(n 3 ) and the execution time is very long. . . Then I looked at the official problem solution and I knew that I decided to have a better
solution. The idea of ​​this solution is also very simple (I didn't think of it before, but I heard it for the first time). Enumerate the center of all strings and expand from the center (of course, two pointers are needed)
But here is a problem. The center of the parity string is different. When it is an odd string, its center is one character. When it is an even string, its center is two strings
. There are two solutions: one is the odd-even case. Cite (I didn’t try this
one ) The other is: List the number (the number refers to the number that starts from 0) and the enumeration center instance to find the law: left=i/2 (rounded down) right=left+i %2 also find out the relationship between the length of the string and the maximum number (that is, how many possible situations are there in total) =2*n-1

(n=4)
   编号                     left                   right
	0			 	     	 0						0
	1						 0						1
	2					     1						1
	3				 		 1						2
	4						 2						2
	5						 2						3
	6						 3						3
class Solution {
    
    
public:
    int countSubstrings(string s) {
    
    
        int n = s.size();
        int ans=0;
        for(int i=0;i<2*n-1;++i){
    
    
            int left=i/2;
            int right=left+i%2;
            while(left>=0&&right<n){
    
    
                if(s[left]==s[right]){
    
    
                    left--;
                    right++;
                    ans++;
                }else{
    
    
                    break;
                }
            }
        }
        return ans;
    }
};

There is another way. . . I heard it for the first time: I learned about the Manacher algorithm online and didn't figure out the principle. Although the code is written, it is written according to the algorithm. . . Record a coordinate
Manacher algorithm
algorithm problem solution

class Solution {
    
    
public:
    int countSubstrings(string s) {
    
    
        int ans=0;
        string t="$#";
        for(const char& c:s){
    
    
            t+=c;
            t+='#';
        }
        t+='!';
        int n = t.size()-1;
        vector<int> flag(n);
        int imax=0;
        int rmax=0;
        for(int i=1;i<n;++i){
    
    
            if(i<=rmax){
    
    
                flag[i]=min(flag[2*imax-i],rmax-i+1);
            }else{
    
    
                flag[i]=1;
            }
            while(t[i+flag[i]]==t[i-flag[i]]){
    
    
                flag[i]++;
            }
            if(flag[i]-1+i>rmax){
    
    
                imax=i;
                rmax=flag[i]-1+i;
            }
            ans+=(flag[i]/2);
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/li_qw_er/article/details/108093628
75